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.
# 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)) |
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)) |
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'])) |
The initialized lists are : List 1 : [1] List 2 : [2] List 3 : [3] List 4 : [4]
Recommended Posts:
- Python | Initializing dictionary with empty lists
- Python | Intersection of multiple lists
- Python | Append multiple lists at once
- Python | Iterate over multiple lists simultaneously
- Python | Interleave multiple lists of same length
- Python | Program to count number of lists in a list of lists
- Python | Zipping two lists of lists
- Multiple Exception Handling in Python
- Python | How to use Multiple kv files in kivy
- Rename multiple files using Python
- Returning Multiple Values in Python
- Python | Replace multiple characters at once
- Python | Add similar value multiple times in list
- Python | Remove multiple keys from dictionary
- Remove multiple elements from a list in Python
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.



