thenickname thenickname - 1 year ago 59
Python Question

Python - Initializing Multiple Lists/Line

This is terribly ugly:

psData = []
nsData = []
msData = []
ckData = []
mAData = []
RData = []
pData = []


Is there a way to declare these variables on a single line?

Answer Source
alist, blist, clist, dlist, elist = ([] for i in range(5))

The downside of above approach is, you need to count the number of names on the left of = and have exactly the same number of empty lists (e.g. via the range call, or more explicitly) on the right hand side.

The main thing is, don't use something like

alist, blist, clist, dlist, elist = [[]] * 5

nor

alist = blist = clist = dlist = elist = []

which would make all names refer to the same empty list!