46

How can I make many empty lists without manually typing the following?

list1=[], list2=[], list3=[]

Is there a for loop that will make me 'n' number of such empty lists?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sameer Patel
  • 927
  • 3
  • 16
  • 20

8 Answers8

140

A list comprehension is easiest here:

>>> n = 5
>>> lists = [[] for _ in range(n)]
>>> lists
[[], [], [], [], []]

Be wary not to fall into the trap that is:

>>> lists = [[]] * 5
>>> lists
[[], [], [], [], []]
>>> lists[0].append(1)
>>> lists
[[1], [1], [1], [1], [1]]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eric
  • 95,302
  • 53
  • 242
  • 374
58

If you want to create different lists without a "list of lists", try this:

list1, list2, list3, list4 = ([] for i in range(4))
alemangui
  • 3,571
  • 22
  • 33
17

Look up list comprehensions:

listOfLists = [[] for i in range(N)]

Now, listOfLists has N empty lists in it.

More links on list comprehensions:

1 2 3

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
7

I see that many answers here get you many lists with no names assigned (as elements of a larger list). This may be not always what's needed.

It is possible to create multiple empty lists, each with a different name/value, using a tuple:

a,b,c = ([],[],[])

Changing this structure should get you what you want.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pauli
  • 170
  • 3
  • 9
4

Use this:

def mklist(n):
    for _ in range(n):
        yield []

Usage:

list(mklist(10))
[[], [], [], [], [], [], [], [], [], []]

a, b, c = mklist(3) # a=[]; b=[]; c=[]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yutaka2487
  • 1,926
  • 2
  • 13
  • 12
4

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)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Martijn Luyckx
  • 402
  • 2
  • 12
  • 1
    `exec` is almost always a really bad idea. If you think about using this code, then change your mind and don't. At the very least, use `globals()[varname] = val`, if you must dynamically create variables – Eric Nov 13 '15 at 02:46
3

Or as simple as

added, modified, removed, inactive = ([],) * 4
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Aleem
  • 549
  • 6
  • 13
1

You can also add a list of list of list of list... like this too. It won't return any error. But I haven't got any idea what it's used for.

n = 5 
>>> lists = [[[]]] for _ in range(n)]
>>> lists
   [[[[]]], [[[]]], [[[]]], [[[]]], [[[]]]]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131