Instruction
Good job! Before we wrap this section up, let's have a quick summary and a quiz. What did we learn?
- Nested lists are lists within lists. They may or may not be rectangular and can consist of as many dimensions as needed, but two-dimensional nested lists are the most popular.
- To access a specific element in a nested list, use as many square bracket pairs as there are dimensions in the list:
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] nested_list[1][2] # returns 6
- To iterate over the values of a nested list, use:
for row in nested_list: for cell in row: ... - To iterate over the indexes of a nested list, use:
for i in range(len(nested_list)): for j in range(len(nested_list[i])): ... - To modify the elements of a nested list, iterate over the indexes, not the values.
- To quickly copy a nested list, use:
import copy new_nested_list = copy.deepcopy(nested_list)
How about that short quiz now?
Exercise
Click to continue.



