Creating a list of lists in python is a little tricky. In this article, we will discuss 4 different ways to create and initialize list of lists.

Wrong way to create & initialize a list of lists in python

Let’s start from basic, quickest way to create and initialize a normal list with same values in python is,

# Creating a list with same values
list_of_num = [5]* 10

print(list_of_num)

Output:
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]

It created a list of size 10 with the same element i.e. 5. Basically it copied the object inside [], 10 times. Let’s use same logic to create and initialize a list of lists,
# Create a list with 4 references of same sub list
list_of_num = [[]] * 4

print('List of lists:')
print(list_of_num)

Output:
[[], [], [], []]

Here we provided an empty list [] inside the [] and multiplied it by 4. It created 4 different references of [] and inserted them into a new list. Yes, these are not 4 empty lists inside the main list, but these are not different list objects instead these are mere references of first list. Let’s confirm this by iterating over the list and printing id of each sub list object,
for elem in list_of_num:
    print(id(elem))

Output:
200792200
200792200
200792200
200792200

All entries in the list have the same ID i.e. they point to the same list object.

Why does it matter ?

Let’s inserting an element in the 3rd sub list of main list i.e.

# Insert 11 into the 3rd sub list
list_of_num[2].append(11)

Now check the contents of the main list,
print('Modified list of lists:')
print(list_of_num)

Output:
Modified list of lists:
[[11], [11], [11], [11]]

Element was inserted in all the sub lists, because these are not different lists. We didn’t expect this at first place, we just wanted to insert the element in the 3rd sub-list. So, it proves that this is a wrong way to create and initialize a list of list. Let’s look at the correct way,

Correct way to Create & Initialize a list of lists in python

Let’s see 4 different but correct ways to create and initialize a list of lists

Use for loop to Create & Initialize list of lists

Suppose we want to create a list, which internally contains 5 different sub lists. To do that, first we will create an new empty list,then we will iterate from numbers 0 to 4 using a for loop and in each iteration, we will append an empty list to the new list i.e.

# Create an empty list
list_of_lists = []
# Iterate over a sequence of numbers from 0 to 4
for i in range(5):
    # In each iteration, add an empty list to the main list
    list_of_lists.append([])

print('List of lists:')
print(list_of_lists)

Output:
List of lists:
[[], [], [], [], []]

Now let’s confirm that if all the sub lists in main list are actually different lists,
for elem in list_of_lists:
    print(id(elem))

Output:
200792232
200792296
200792168
200740648
200740680

All the sub lists have different IDs, which confirms that these are different objects.

Let’s insert the element in the 3rd sub list,

# Insert 11 into the 3rd sub list
list_of_lists[2].append(11)

print(list_of_lists)

Output:
[[], [], [11], [], []]

Element is added only in the 3rd sub list, all other sub lists are not affected. So, it also confirms that all the sub lists are not the references of the same list, instead they are completely different objects.

Use List Comprehension & range() to create a list of lists

Using Python’s range() function, we can generate a sequence of numbers from 0 to n-1 and for each element in the sequence create & append a sub-list to the main list using List Comprehension i.e.

# Create a list of 5 empty sub lists
list_of_lists = [[] for i in range(5)]

print('List of lists:')
print(list_of_lists)

Output:
List of lists:
[[], [], [], [], []]

It created a list with 5 sub lists. Let’s confirm if each sub-list is a different object or not,
for elem in list_of_lists:
    print(id(elem))

Output:
200739688
200739944
200739848
200739912
200739880

It proves that all sub lists have different Identities.

Use List Comprehension & repeat() to create a list of lists

In python itertools module provide a function repeat(),

itertools.repeat(object[, N])

It returns an Iterator which in turns returns the given object N times.
We can use it for basic iteration from 0 to 4 and in each iteration we will append a sublist to main list i.e.
from itertools import repeat

num = 5
# Use list comprehension to create a list of 5 sub lists
list_of_lists = [[] for x in repeat(None, num)]

print('List of lists:')
print(list_of_lists)

Output:
List of lists:
[[], [], [], [], []]

It returned a list with 5 sub-lists. We can also confirm that all the sub lists are different object,
for elem in list_of_lists:
    print(id(elem))

Output:
200792264
200792232
200792296
200792168
200740648

It proves that all sub lists have different Identities.Although, this is one of the least used method to create a list of lists, but still it is good to know different things because once you know the APIs and their use cases, then you can apply it to some other scenarios.

Use Numpy to create a list of lists

Numpy module in python provides a function empty() to create a empty Numpy array of given shape i.e.

numpy.empty(shape, dtype=float, order='C')

It returns a new Numpy array of given shape.

So, now to create a list of lists, we will create a 2D Numpy array using empty() function and will then convert it into a list of lists using numpy.tolist() function. For example,

import numpy 

num = 5
# Create a 2D Numpy array of shape (5, 0) and convert it to list of lists
list_of_lists = numpy.empty((num, 0)).tolist()

print('List of lists:')
print(list_of_lists)

Output:
List of lists:
[[], [], [], [], []]

So, we create a list that has 5 sublists. Now let’s confirm that all the sub lists are different object,
for elem in list_of_lists:
    print(id(elem))

Output:
200740616
200739688
200739944
200739848
200739912

It proves that all sub lists are different objects.

The complete example is as follows,

import numpy
from itertools import repeat


def main():
    # Creating a list with same values
    list_of_num = [5]* 10
    print(list_of_num)

    print("**** Wrong way to create a List of lists ****")

    # Create a list with 4 references of same sub list
    list_of_num = [[]] * 4

    print('List of lists:')
    print(list_of_num)

    print('Çheck if all sub lists have same identities')

    for elem in list_of_num:
        print(id(elem))

    print('Try inserting an element in the 3rd sub list')

    # Insert 11 into the 3rd sub list
    list_of_num[2].append(11)

    print('Modified list of lists:')
    print(list_of_num)

    print("**** Correct way to create a List of lists ****")

    print('*** Use for loop to Create & Initialize list of lists ***')

    # Create an empty list
    list_of_lists = []
    # Iterate over a sequence of numbers from 0 to 4
    for i in range(5):
        # In each iteration, add an empty list to the main list
        list_of_lists.append([])

    print('List of lists:')
    print(list_of_lists)

    print('Çheck if all sub lists have different identities')
    for elem in list_of_lists:
        print(id(elem))

    print('Append an element to the 3rd sub list')

    # Insert 11 into the 3rd sub list
    list_of_lists[2].append(11)

    print('Modified List of lists:')
    print(list_of_lists)

    print('*** Use List Comprehension & range() to create a list of lists ***')

    # Create a list of 5 empty sub lists
    list_of_lists = [[] for i in range(5)]

    print('List of lists:')
    print(list_of_lists)

    print('Çheck if all sub lists have different identities')
    for elem in list_of_lists:
        print(id(elem))

    print('*** Use List Comprehension & repeat() to create a list of lists ***')

    num = 5
    # Use list comprehension to create a list of 5 sub lists
    list_of_lists = [[] for x in repeat(None, num)]

    print('List of lists:')
    print(list_of_lists)

    print('Çheck if all sub lists have different identities')
    for elem in list_of_lists:
        print(id(elem))

    print('*** Use Numpy to create a list of lists ***')

    num = 5
    # Create a 2D Numpy array of shape (5, 0) and convert it to list of lists
    list_of_lists = numpy.empty((num, 0)).tolist()

    print('List of lists:')
    print(list_of_lists)

    print('Check if all elements are different ?')
    for elem in list_of_lists:
        print(id(elem))


if __name__ == '__main__':
   main()

Output:
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
**** Wrong way to create a List of lists ****
List of lists:
[[], [], [], []]
Çheck if all sub lists have same identities
200792200
200792200
200792200
200792200
Try inserting an element in the 3rd sub list
Modified list of lists:
[[11], [11], [11], [11]]
**** Correct way to create a List of lists ****
*** Use for loop to Create & Initialize list of lists ***
List of lists:
[[], [], [], [], []]
Çheck if all sub lists have different identities
200792232
200792296
200792168
200740648
200740680
Append an element to the 3rd sub list
Modified List of lists:
[[], [], [11], [], []]
*** Use List Comprehension & range() to create a list of lists ***
List of lists:
[[], [], [], [], []]
Çheck if all sub lists have different identities
200739688
200739944
200739848
200739912
200739880
*** Use List Comprehension & repeat() to create a list of lists ***
List of lists:
[[], [], [], [], []]
Çheck if all sub lists have different identities
200792264
200792232
200792296
200792168
200740648
*** Use Numpy to create a list of lists ***
List of lists:
[[], [], [], [], []]
Check if all elements are different ?
200740616
200739688
200739944
200739848
200739912

Join a list of 2000+ Programmers for latest Tips & Tutorials