Javaexercise.com

How to Create a List of Lists or Nested List in Python?

This article is all about creating and initializing a list of lists in Python.

A list of lists is basically a nested list that contains one or more lists inside a list. It is also known as a nested list.

There are many approaches to create a list of lists. Here, we are using the append() method and list comprehension technique to create a list of lists.

After creating a list of lists, we will see to access list elements as well. Let's see some examples.

Create a list of lists using the append() method in Python

In this example, we are using an append() method that is used to append a list into a list as an element.

We created two lists and append them into another list using the append() method and print the list which is actually a list of lists. 

# Take two lists
list1 = [1,2,3,4]
list2 = [5,6,7,8]
list3 = [] # Take an empty list
# make list of lists
list3.append(list1)
list3.append(list2)
print(list3)

Output:

[[1, 2, 3, 4], [5, 6, 7, 8]]

Create a list of lists using the list initializer in Python

The list initializer syntax is used to create a list in Python.

We can use this technique to create a list of lists by passing lists as elements into the list initializer. See the code and output.

# Take two lists
list1 = [1,2,3,4]
list2 = [5,6,7,8]
# make list of lists
list3 = [list1, list2]
# Display result
print(list3)

Output:

[[1, 2, 3, 4], [5, 6, 7, 8]]

Create a list of lists by using for-loop in Python

We can use for loop to create a list of lists in Python as well.

For this, we can use the append() method inside the loop to add the element into the list to form a list of lists. See the code and output.

lists = []
# make list of lists
for i in range(2): 
    # append list 
    lists.append([]) 
    for j in range(5): 
        lists[i].append(j) 
# Display result
print(lists)

Output:

[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]

Create a list of lists using list comprehension in Python

If you are comfortable with list comprehension then use it to make a list of lists as we did in the below code example. See the code and output here.

# Take a list
list = ['Apple','Mango','Orange']
lists = []
# make list of lists
lists = [[val] for val in list] 
# Display result
print(lists)

Output:

[['Apple'], ['Mango'], ['Orange']]

How to access elements from a list of lists in Python

After creating a list of lists, now let's learn to access items from it.

For this purpose, we can use the indexing approach which means we can access elements by using an index.

The list index starts from 0 and ends at n-1 where n is the length of the list.

Here, we used 0 index to get the first element of the list.

# Take a list
list = ['Apple','Mango','Orange']
lists = []
# make list of lists
lists = [[val] for val in list] 
# Display result
print(lists)
# Access Element
print(lists[0])
print(lists[2])

Output:

[['Apple'], ['Mango'], ['Orange']]
['Apple']
['Orange']

 

Let's see one more example where we are accessing elements of the nested list. If your nested list has more elements then you can use this code.

# Take a list
list1 = ['Apple','Mango','Orange']
list2 = ['Banana','Pineapple','Sapota']
lists = []
# make list of lists
lists = [list1,list2] 
# Display result
print(lists) # list of lists

print(lists[0]) # first list
print(lists[1]) # second list

# Access Elements
print(lists[0][0]) # first element of first list
print(lists[1][0]) # first element of the second list

print(lists[0][2]) # last element of first list
print(lists[1][2]) # last element of the second list

Output:

[['Apple', 'Mango', 'Orange'], ['Banana', 'Pineapple', 'Sapota']]
['Apple', 'Mango', 'Orange']
['Banana', 'Pineapple', 'Sapota']
Apple
Banana
Orange
Sapota

 

Also Read: Tuple of Tuples - Nested Tuple in Python