How to Create List of Lists in Python


The list of lists is a data structure in Python that consists of a list containing other(nested) lists. It is a two-dimensional data structure, similar to a matrix in mathematics. Python offers various methods to create a list of lists, such as List comprehension, append() function, for-loop, numpy, etc.

This blog post will teach you how to create/define a list of lists in python via three different approaches.

How to Define/Create a List of Lists Via Python’s append() Function?

Following are the steps to create a list of lists using the append() function in Python:

Step 1:

Create a variable and assign an empty list to it. This will be the outer/main list containing various inner lists.

list_of_lists = []

Step 2:

Create a new inner list and append it to the outer list using the append() function.

inner_list_1 = [12, 72, 32, 64]
list_of_lists.append(inner_list_1)

Repeat the process to create additional inner lists and append them to the outer list.

inner_list_2 = [14, 12, 5, 89, 0]
list_of_lists.append(inner_list_2)

inner_list_3 = [17, 81, 19]
list_of_lists.append(inner_list_3)

Step 3:

To access/print a list of lists, execute the following code.

print(list_of_lists)

Step 4:

You can also access the inner lists using indexing. For example, to access the third inner list, you must use the list_of_lists[2].

print(list_of_lists[2])

Complete Code Snippet:

Output:

The above image shows the complete list of lists and an inner list having index “2”.

How to Create/Define a List of Lists Via List Comprehension in Python?

To create a list of lists in Python, use the list comprehension method with the collaboration of the for-loop and the range() function. For this, you can follow these steps:

Step 1:

Create a List

list = [100, 12, 14, 72]

Step 2:

Next, utilize the List comprehension approach to create nested lists.

list_comp = [list for i in range(4)]

Step 3:

Print/Access the nested lists.

print(list_comp)

Complete Code Snippet:

Output:

The above image shows that a list of lists has been created.

How to Create/Define a List of Lists via the Traditional For Loop in Python?

To create a list of lists using a for loop, you can follow these steps:

Step 1:

Initialize an empty list that will store the sublists

list_of_lists = []

Step 2:

Use a for loop to iterate over a range or a list of items.

for i in range(3):

Step 3:

For each iteration of the for loop, create a new list and append it to the list of lists using the append() method.

sublist = []
list_of_lists.append(sublist)

Step 4:

Inside the for loop, you can add items to the sublist using the append() method or slicing and indexing.

sublist.append(i)
sublist.append(i)
sublist.append(i)

Complete Code Snippet:

Output:

The above image shows that a list of lists has been created.

Conclusion

Different methods are used to create a list of lists in python, such as the append() function,  List comprehension, for-loop, numpy, etc. A list of lists is a two-dimensional Python data structure that contains other lists as its elements. This python blog taught us how to create a list of lists using three different approaches.

Print Friendly, PDF & Email