I wish to create a new list each time a for loop runs
#Reading user input e.g. 10
lists = int(raw_input("How many lists do you want? "))
for p in range(0,pooled):
#Here I want to create 10 new empty lists: list1, list2, list3 ...
Is there any smart way of doing this?
Thanks,
Kasper
Using for Loops You can use a for loop to create a list of elements in three steps: Instantiate an empty list. Loop over an iterable or range of elements. Append each element to the end of the list.
You can simply use a for loop to create n lists. The variable a_i changes as i increments, so it becomes a_1, a_2, a_3 ... and so on.
Use a list comprehension:
num_lists = int(raw_input("How many lists do you want? "))
lists = [[] for i in xrange(num_lists)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With