Skip to Content

How to Make a List of Lists in Python – Easy!

How to Make a List of Lists in Python – Easy!

Today, we will see how to create a list of lists in Python. There are multiple ways to do that. Let’s get started and see some of the methods.

 

For Loop and the append() method

The first method is quite simple and straightforward. Initially, we create an empty list lst1, then we run a loop and append lists to lst1.

If we want to insert n sublists, then we will have to run a loop n times using the range() function. Let’s understand this concept using an example.

 

lst1 = []
for i in range(0, 5):
  lst1.append([])

print(lst1)

 

Output

 

[[], [], [], [], []]

 

Here, the loop runs five times. In each iteration, we append an empty list to lst1, which allows us to create a list of lists, as you can see in the output.

 

List Comprehension

Another method is to use list comprehension that provides us an easier and a concise way. Let’s see an example.

 

lst  = [[] for i in range(0, 5)]
print(lst)

 

Output

 

[[], [], [], [], []]

 

A list comprehension always returns a list whose contents depend upon the expressions in the for loop and if condition (if any).

In the above example, we add a sublist every time the loop runs, and thus, the result contains a list of lists.

 

NumPy Library

Another method to make a list of lists is to NumPy. It is a powerful library for scientific computations.

It provides several methods and tools to create and work with multidimensional arrays efficiently.

We can make a list of lists using the empty() method of the NumPy library. We need to pass a tuple containing the row size and the column size.

It also takes a data type. By default, it will create an array of type numpy.float64.

Moreover, it returns a ndarray (N-Dimensional array) of fixed size and type. To convert it to a list, we will use the tolist() method.

Consider the following code:

 

import numpy as np
np_array = np.empty((5, 0))
lst = np_array.tolist()
print(lst)
print(f"Type of np_array: {type(np_array)} and the type of lst: {type(lst)}")

 

Output

 

[[], [], [], [], []]
Type of np_array: <class 'numpy.ndarray'> and the type of lst: <class 'list'>

 

We can do the same using the numpy.ndarray() method. Let’s see.

 

import numpy as np
np_array = np.ndarray((5, 0))
lst = np_array.tolist()
print(lst)

 

 

[[], [], [], [], []]

 

The map() function

We can also create a list of lists using Python’s built-in map() function. map() takes two arguments: a function and an iterable.

It calls the given function for each item of an iterable and returns an iterator. Consider the following example.

 

n=5
lst = [None]*n
lst = list(map(lambda x: [], lst))
print(lst)

 

Output

[[], [], [], [], []]

 

First, we create a list of n elements containing None. Then, we pass this list to map().

Each item of the outer list gets mapped to an empty list using the anonymous function. Finally, we convert the returned iterator (map object) to a list to get a list of lists.

 

What not to do

We can create a one-dimensional list in the following way.

 

lst = [None]*n

 

Here, lst will be of size n, and each item will have the value None. In other words, any value we put inside the square brackets get repeated n times.

So if we put [] inside it, then we will get a list of lists, no? Well, you do, but every item refers to the same object (the first one). Simply put, we get n same sublists. let’s see.

 

n=5
lst = [[]]*n
print(lst)
#append an item to the last list
lst[n-1].append(3)
print(lst)

 

Output

 

[[], [], [], [], []]
[[3], [3], [3], [3], [3]]

 

Consider another code.

 

n=5
lst = []
new_list =  [lst for i in range(0, n)]
#append an item to the last list
new_list[n-1].append(3)
print(lst)
print(new_list)

 

This above code also creates five references of the variable lst. So, new_list[0], new_list[1], … new_list[n-1] refer to the same address pointed by lst.

 

Output

 

[3]
[[3], [3], [3], [3], [3]]

 

Use lst[:] instead of lst or copy the list explicitly using copy() from the copy module.