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
list1, list2, list3, list4 = ([] for i in range ( 4 ))
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
import collections
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 )
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
list1, list2, list3, list4 = ([], ) * 4
list1.append( "hello there" )
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
list1, list2, list3, list4 = map ( lambda x: list (x), repeat([], 4 ))
list1.append( 1 )
list2.append( 2 )
list3.append( 3 )
list4.append( 4 )
print (list1)
print (list2)
print (list3)
print (list4)
|
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' ])
print (lists[ 'list2' ])
print (lists[ 'list3' ])
print (lists[ 'list4' ])
|
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 Apr, 2023
Like Article
Save Article