Python - how to generate list of variables with new number at end

SchoonSauce picture SchoonSauce · Nov 7, 2013 · Viewed 27.7k times · Source

What I would like to do is find a more concise way of creating variables of empty lists that will be similar to each other, with the exception of different numbers at the end.

#For example:
var1 = []
var2 = []
var3 = []
var4 = []
#...
varN = []

#with the end goal of:
var_list = [var1,var2,var3,var4, ... varN]

Answer

vitiral picture vitiral · Nov 7, 2013

Use a list comprehension:

[[] for n in range(N)]

or a simple for loop

empt_lists = []
for n in range(N):
    empt_lists.append([])

Note that [[]]*N does NOT work, it will use the same pointer for all the items!!!