The code below will dynamically 'create' itself. For every iteration, the following command will issued:
listnumber = []
Where number is the value of i in the loop.
x = 3 # Amount of lists you want to create for i in range(1, x+1): command = "" # This line is here to clear out the previous command command = "list" + str(i) + " = []" exec(command)The result of this particular piece of code is three variables: list1, list2 and list3 being created and each assigned an empty list.
You can generalize this to make practically anything you want. Do note that you cannot put this into a function as far as I know, because of how variables and global variables work.
name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created. value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'" amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5). for i in range(1, amount+1): command_variable = "" command_variable = name + str(i) + " = " + value exec(command_variable)View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
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 : []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]Method 4: Using * operator:
Itdoes 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"]