Python | Initializing multiple lists

In real applications, we often have to work with multiple lists and to 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 type and number of lists declared to be used.

Method #1 : Using * operator
We can enlist all the required list comma separated and then initialize them with an empty list and multiply that empty list using the * operator by the number of lists specified.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# to initialize multiple lists
# using * operator
  
# using * operator
# to initialize multiple lists
list1, list2, list3, list4 = ([], ) * 4
  
# printing lists
print ("The initialized lists are : ")
print ("List 1 : " + str(list1))
print ("List 2 : " + str(list2))
print ("List 3 : " + str(list3))
print ("List 4 : " + str(list4))

chevron_right


Output:

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

 
Method #2 : Using loop
This method is similar to the method explained above but the only difference is that we use loop instead of * operator to perform the task of multiple assignments.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 ("The initialized lists are : ")
print ("List 1 : " + str(list1))
print ("List 2 : " + str(list2))
print ("List 3 : " + str(list3))
print ("List 4 : " + str(list4))

chevron_right


Output:

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

 
Method #3 : Using defaultdict()
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 option to make any number of keys and perform the append operations straight away as they get initialized by the list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# to initialize multiple lists
# using defaultdict()
import collections
  
# using defaultdict()
# 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 ("The initialized lists are : ")
print ("List 1 : " + str(mul_list_dict['list1']))
print ("List 2 : " + str(mul_list_dict['list2']))
print ("List 3 : " + str(mul_list_dict['list3']))
print ("List 4 : " + str(mul_list_dict['list4']))

chevron_right


Output:

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.