Python

Create a List of Lists in Python

There is no constraint on the type of value that can be stored inside a list in Python. This means that the list variable in Python can actually store another list, or multiple lists as the elements of the list. However, when new programmers are getting started with the list inside a list, they often get confused. Therefore, this post will work as a guide and explain how to create a list inside another list in Python.

Let’s get started with the first method to create a list inside a list.

Method 1: Using the List Initializer

The easiest and most fundamental way of creating a list and even a nested list is by using the list initializer notation “[ ]”. For the nested list, simply use the square brackets at the inside list again. To demonstrate this, let’s create a nested list of numbers using the following lines of code:

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

 
As you can see, the list “listVar” contains two elements, and both of these elements are individual lists containing numeric values. To verify this, simply print out the first element of the listVar variable and print out its type by using the type() method as well:

print(listVar[1])
print(type(listVar[1]))

 
When this is executed, it produces the following output on the terminal:


As you can see, the values of the first element (listVar) have been printed and the type is shown as “list”.

Moreover, two separate lists can be used to create a nested list by using the list initializing notation. To demonstrate this, use the following code snippet:

listVar_1 = [1,2,3,4]
listVar_2 = [5,6,7,8]

listVar_1=  [listVar_1, listVar_2]

print(listVar_1)

 
When this code is executed, it produces the following result on the terminal:


The output verifies that you have created a nested list or a list of lists using the initializing list notation.

Method 2: Using the append() Method

The append() method can be used to append a list to another list. To demonstrate this method, take the following code snippet:

listVar_1 = [1,2,3,4]
listVar_2 = [5,6,7,8]
listVar_3= [10,20,30]

listVar_3.append(listVar_1)
listVar_3.append(listVar_2)

print(listVar_3)

 
In this above code snippet:

    • Three different list variables are created and the goal is to append() the first two lists into the third list variable.
    • The append() method is called on the third list “listVar_3” using the dot-operator and in the argument the lists “listVar_1” and the “listVar_2” is passed.
    • Lastly, the listVar_3 is printed on the terminal.

When this code is executed, it produces the following result on the terminal:


In the output, you can observe that the first three elements are numeric values. However, the fourth and the fifth variables are individual lists, meaning you have successfully created a list of lists.

Conclusion

If you are looking for a way to create a list of lists in Python, then in that case, utilize the list initialization notation, which uses square brackets to create lists, or use the append() method to append already existing lists into another list. Both of these methods have been demonstrated thoroughly in this article.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.