Open In App

Python | Initializing multiple lists

Last Updated : 18 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In real applications, we often have to work with multiple lists, and initialize them with empty lists hampers the readability of code. Hence a one-liner is required to perform this task in short so as to give a clear idea of the type and number of lists declared to be used.

Method #1: Using loops

We can enlist all the required list comma separated and then initialize them with a loop of empty lists. 

Python3




# Python3 code to demonstrate
# to initialize multiple lists
# using loop
 
# using loop
# to initialize multiple lists
list1, list2, list3, list4 = ([] for i in range(4))
 
# printing lists
print (& quot
        The initialized lists are : & quot
        )
print (& quot
        List 1 : & quot
        + str(list1))
print (& quot
        List 2 : & quot
        + str(list2))
print (& quot
        List 3 : & quot
        + str(list3))
print (& quot
        List 4 : & quot
        + str(list4))


Output:

The initialized lists are : 
List 1 : []
List 2 : []
List 3 : []
List 4 : []

Time complexity: O(n), where n is the number of lists to be initialized.

Auxiliary space: O(n), where n is the number of lists to be initialized.

Method #2: Using defaultdict() Method 

This is a method different and also performs a slightly different utility than the above two methods discussed. This creates a dictionary with a specific name and we have the option to make any number of keys and perform the append operations straight away as they get initialized by the list. 

Python3




# Python3 code to demonstrate
# to initialize multiple lists
# using defaultdict()
import collections
 
# using defaultdict() method
# to initialize multiple lists
# no need to initialize with empty lists
mul_list_dict = collections.defaultdict(list)
mul_list_dict['list1'].append(1)
mul_list_dict['list2'].append(2)
mul_list_dict['list3'].append(3)
mul_list_dict['list4'].append(4)
 
# printing lists
print (& quot
        The initialized lists are : & quot
        )
print (& quot
        List 1 : & quot
        + str(mul_list_dict['list1']))
print (& quot
        List 2 : & quot
        + str(mul_list_dict['list2']))
print (& quot
        List 3 : & quot
        + str(mul_list_dict['list3']))
print (& quot
        List 4 : & quot
        + str(mul_list_dict['list4']))


Output:

The initialized lists are : 
List 1 : [1]
List 2 : [2]
List 3 : [3]
List 4 : [4]

Time Complexity: O(n), where n is the length of the input list. This is because we’re using defaultdict() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space.

Method #3: Using * operator: 

It does not create independent lists, but variables referring to the same (empty) list! 

Python3




# Python3 code to demonstrate
# how not to initialize multiple lists
 
# using * operator
# to initialize multiple pointers to the same list
list1, list2, list3, list4 = ([], ) * 4
 
# change only list1
list1.append("hello there")
 
# printing lists
print (& quot
        The initialized lists are all the same: & quot
        )
print (& quot
        List 1 : & quot
        + str(list1))
print (& quot
        List 2 : & quot
        + str(list2))
print (& quot
        List 3 : & quot
        + str(list3))
print (& quot
        List 4 : & quot
        + str(list4))


Output:

The initialized lists are all the same: 
List 1 : ["hello there"]
List 2 : ["hello there"]
List 3 : ["hello there"]
List 4 : ["hello there"]

Method #4: Using repeat:

To initialize multiple lists using the repeat method, you can do the following:

Python3




from itertools import repeat
 
# Initialize 4 lists with empty lists
list1, list2, list3, list4 = map(lambda x: list(x), repeat([], 4))
 
# You can now use the lists as you normally would
list1.append(1)
list2.append(2)
list3.append(3)
list4.append(4)
 
print(list1)  # [1]
print(list2)  # [2]
print(list3)  # [3]
print(list4)  # [4]
#This code is contributed by Edula Vinay Kumar Reddy


Output

[1]
[2]
[3]
[4]

The time complexity of this method is O(n), where n is the number of lists you want to create. 

 Method 5: use a dictionary with list values to store the lists. 

This method allows for easy access to the lists using their keys and avoids the need to create four separate variables. It can be particularly useful when dealing with a large number of lists or when the number of lists is not known beforehand.

Python3




lists = {'list1': [], 'list2': [], 'list3': [], 'list4': []}
 
lists['list1'].append(1)
lists['list2'].append(2)
lists['list3'].append(3)
lists['list4'].append(4)
 
print(lists['list1'])  # [1]
print(lists['list2'])  # [2]
print(lists['list3'])  # [3]
print(lists['list4'])  # [4]


Output

[1]
[2]
[3]
[4]

Time complexity: O(1), so the total time complexity of the four operations is also O(1).
Auxiliary space: O(1) because the size of the dictionary does not depend on the size of the input data. 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads