Python List of Lists

Python List of Lists

Python List of Lists is a Python list containing elements that are Lists.

We know that a Python List can contain elements of any type. So, if we assign Python lists for these elements, we get a Python List of Lists.

Python List of Lists

Python List of Lists is similar to a two dimensional array. Inner lists can have different sizes.

Create List of Lists in Python

1. Create List of Lists using List Initializer

In the following program, we create a list containing lists as elements. We shall use list initializer (square bracket) notation.

Python Program

list_of_lists = [['apple', 'banana', 'cherry'],
                  [100, 200, 300],
                  [3.14, 5.87, 9.11]]

print(list_of_lists)
Run Code Copy

Output

[['apple', 'banana', 'cherry'], [100, 200, 300], [3.14, 5.87, 9.11]]

The type of elements that we store inside the inner list can be independent of others.

2. Create List of Lists using List append()

We can also use List append() function, to create an empty outer list, and then add inner lists.

Python Program

list_1 = ['apple', 'banana', 'cherry']
list_2 = [100, 200, 300]
list_3 = [3.14, 5.87, 9.11]

# Create an empty list of lists
list_of_lists = []

# Append inner lists to the outer list
list_of_lists.append(list_1)
list_of_lists.append(list_2)
list_of_lists.append(list_3)

print(list_of_lists)
Run Code Copy

Output

[['apple', 'banana', 'cherry'], [100, 200, 300], [3.14, 5.87, 9.11]]

3. Create List of Lists using List Comprehension

We can also use List Comprehension to create a list of lists.

For example, in the following program, we create a list of lists with integer values as items in the inner lists. This would appear like a matrix.

Python Program

rows = 3
cols = 4
matrix = [[x*y for x in range(cols)] for y in range(rows)]

for row in matrix:
    print(row)
Run Code Copy

Output

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

Iterate over inner lists

You can use looping statements like While loop, or For loop to iterate over the inner lists.

In the following program, we shall use while loop to iterate over inner lists.

Python Program

list_of_lists = [['apple', 'banana', 'cherry'],
                  [100, 200, 300],
                  [3.14, 5.87, 9.11]]

i = 0
while i < len(list_of_lists):
    print(list_of_lists[i])
    i += 1
Run Code Copy

Output

['apple', 'banana', 'cherry']
[100, 200, 300]
[3.14, 5.87, 9.11]

Also, in the following example, we shall learn how to use for loop to iterate over the inner lists of Python list of lists.

Python Program

list_of_lists = [['apple', 'banana', 'cherry'],
                  [100, 200, 300],
                  [3.14, 5.87, 9.11]]

for list_i in list_of_lists:
    print(list_i)
Run Code Copy

Output

['apple', 'banana', 'cherry']
[100, 200, 300]
[3.14, 5.87, 9.11]

Iterate through items of inner lists

To iterate over the items of inner lists in a list of lists, you can use nested loop statements.

For example, in the following program, we take a list of lists in list_of_lists, and then use a nested For loop where the outer For loop iterates through the inner lists, and the inner For loop iterates through the items in the inner lists.

Python Program

list_of_lists = [['apple', 'banana', 'cherry'],
                  [100, 200, 300],
                  [3.14, 5.87, 9.11]]

for index, list_i in enumerate(list_of_lists):
    print(f"Iterating over items in inner list {index}")
    for item in list_i:
        print(item)
Run Code Copy

Output

Iterating over items in inner list 0
apple
banana
cherry
Iterating over items in inner list 1
100
200
300
Iterating over items in inner list 2
3.14
5.87
9.11

Remove an inner List from List of Lists

Just like you remove an item from a list, you can remove an inner list from list of lists.

In the following example, we will use del keyword to remove list at index 1.

Python Program

list_of_lists = [['apple', 'banana', 'cherry'],
                  [100, 200, 300],
                  [3.14, 5.87, 9.11]]

del list_of_lists[1]

print(list_of_lists)
Run Code Copy

Output

[['apple', 'banana', 'cherry'], [3.14, 5.87, 9.11]]

Summary

In this tutorial of Python Examples, we learned how to define list of lists in Python, how to traverse the inner lists, how delete an inner list, etc., with example programs.

Related Tutorials

Code copied to clipboard successfully 👍