Nested lists let you store lists inside another list. This helps organize data in groups, like a list of lists.
Nested lists in Python
Start learning this pattern below
Jump into concepts and practice - no test required
nested_list = [
['item1', 'item2', 'item3'],
['item4', 'item5', 'item6'],
['item7', 'item8', 'item9']
]Each item inside the main list can itself be a list.
You can access elements using two sets of square brackets: nested_list[row][column]
empty_nested_list = []
print(empty_nested_list)single_element_nested_list = [[42]] print(single_element_nested_list[0][0])
mixed_nested_list = [[1, 2], [3, 4, 5], []] print(mixed_nested_list[1][2])
access_last_element = nested_list = [[10, 20], [30, 40], [50, 60]] print(nested_list[2][1])
This program creates a nested list like a 3x3 grid, prints it, changes the middle value, and prints it again.
def print_nested_list(nested_list): for row_index, inner_list in enumerate(nested_list): print(f"Row {row_index}: {inner_list}") # Create a nested list representing a 3x3 grid grid = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print("Original nested list:") print_nested_list(grid) # Change the middle element grid[1][1] = 55 print("\nNested list after changing the middle element:") print_nested_list(grid)
Accessing elements in nested lists takes two steps: first the outer list, then the inner list.
Time complexity to access an element is O(1) because lists allow direct access by index.
Be careful with empty inner lists; trying to access elements inside them causes errors.
Use nested lists when you need simple multi-level grouping. For more complex data, consider other structures like dictionaries.
Nested lists store lists inside lists to organize data in multiple levels.
You access elements with two sets of brackets: outer and inner indexes.
They are useful for grids, tables, and grouped data.
Practice
Solution
Step 1: Understand list elements
A list can hold many types of elements, including other lists.Step 2: Define nested list
A nested list is a list where some elements are themselves lists.Final Answer:
A list that contains other lists as its elements -> Option BQuick Check:
Nested list = list inside list [OK]
- Confusing nested list with single-level list
- Thinking nested list is immutable
- Assuming nested list holds only numbers
Solution
Step 1: Identify nested list syntax
A nested list has lists inside the main list, each enclosed in brackets.Step 2: Check options
[[1, 2], [3, 4]] has two inner lists: [1, 2] and [3, 4], correctly nested.Final Answer:
[[1, 2], [3, 4]] -> Option AQuick Check:
Two inner lists = double brackets [OK]
- Using single brackets only
- Mixing elements and lists incorrectly
- Missing commas between inner lists
matrix = [[1, 2], [3, 4]] print(matrix[1][0])
Solution
Step 1: Access outer list element
matrix[1] accesses the second inner list: [3, 4].Step 2: Access inner list element
matrix[1][0] accesses the first element of [3, 4], which is 3.Final Answer:
3 -> Option AQuick Check:
matrix[1][0] = 3 [OK]
- Mixing up indexes (using [0][1] instead)
- Forgetting zero-based indexing
- Confusing inner and outer list positions
data = [[1, 2], [3, 4]] print(data[2][0])
Solution
Step 1: Check list length
data has two elements: indexes 0 and 1 only.Step 2: Accessing invalid index
data[2] tries to access a third element which does not exist, causing IndexError.Final Answer:
IndexError because data[2] does not exist -> Option DQuick Check:
Index out of range = IndexError [OK]
- Assuming index 2 exists
- Confusing syntax error with runtime error
- Ignoring zero-based indexing
nums = [[1, 2, 3], [4, 5], [6]], which code correctly prints all elements in order?Solution
Step 1: Understand nested iteration
To print all elements, loop over each inner list, then each element inside it.Step 2: Analyze options
for sublist in nums: for num in sublist: print(num) uses two loops: outer for sublist, inner for num, printing each number.Final Answer:
for sublist in nums: for num in sublist: print(num) -> Option CQuick Check:
Nested loops print all elements [OK]
- Printing inner lists instead of elements
- Using single loop only
- Hardcoding indexes without loops
