Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a new list for each for loop

Tags:

python

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

like image 571
user838744 Avatar asked Jul 11 '11 11:07

user838744


People also ask

How do you create a new list in loop?

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.

How do you create a multi list in Python?

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.


1 Answers

Use a list comprehension:

num_lists = int(raw_input("How many lists do you want? "))
lists = [[] for i in xrange(num_lists)]
like image 105
Fred Foo Avatar answered Oct 08 '22 00:10

Fred Foo