Skip to content
Related Articles

Related Articles

Python | Initializing multiple lists
  • Last Updated : 30 Dec, 2018

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.




# 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))
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.




# 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))
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.




# 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']))
Output:
The initialized lists are : 
List 1 : [1]
List 2 : [2]
List 3 : [3]
List 4 : [4]

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :