How to Create a List of Lists in Python

To create a list of lists in Python, you can use [] square brackets or the append() function.

The following example shows how to create a list of lists in Python using two different methods.

Using [] Square Bracket

We can use [] square brackets to create a list of lists.

Suppose we have the following data:

# Create list of lists
list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Show list of lists
print(list_of_lists)

Output: 👇️

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

In this example, we use [] square brackets to create a list of lists list_of_lists. The output shows the list of lists created using [] square brackets.

Using append() Function

We can use the append() function to create a list of lists.

Suppose we have the following data:

# Declare empty list
list_of_lists = []

# Add lists
list_of_lists.append([1, 2, 3])
list_of_lists.append([4, 5, 6])
list_of_lists.append([7, 8, 9])

# Show list of lists
print(list_of_lists)

Output: 👇️

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

In this example, we use the append() function to add lists to the list list_of_lists.

The output shows the list of lists created using the append() function.

Conclusion

We can use [] square brackets or the append() function to create a list of lists in Python.