Python - Initializing Multiple Lists/Line Python - Initializing Multiple Lists/Line python python

Python - Initializing Multiple Lists/Line


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!


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


Depending on your needs, you could consider using a defaultdict with a list factory.Something like:

my_lists = collections.defaultdict(list)

and then you can directly append to my_lists["psData"] and so on.This is the relevant doc page: http://docs.python.org/library/collections.html#collections.defaultdict