Python List of Lists
To create a list list of lists in Python, use the native append()
function. Pass the first list before the function and the list to append as the first argument.
Python List of Lists Example
Let's create a list containing two nested lists.
all_numbers = []
numbers_1 = [3, 6]
numbers_2 = [1, 12]
all_numbers.append(numbers_1)
all_numbers.append(numbers_2)
print(all_numbers)
[[3, 6], [1, 12]]
Accessing Nested Lists
To access an element on a nested list, specify the index of the list then the index of the element on that list like this:
items = [[3, 6], [1, 12]]
print(items[1][0])
1