views:

190

answers:

3

This is terribly ugly:

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

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

+8  A: 
alist, blist, clist, dlist, elist = ([] for i in range(5))

the downside 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

or

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

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

Alex Martelli
Awesome, thank you for the response. Tested and working!
thenickname
@thenickname, always glad to help!
Alex Martelli
A: 
psData,nsData,msData,ckData,mAData,RData,pData = [],[],[],[],[],[],[]
S.Mark
seven names on the left of the `=`, six lists on its right -> `ValueError` exception. See why I mention the need to count things as the downside of this approach in my answer?
Alex Martelli
@Alex, Surely my bad, I counted as six last night, LoL
S.Mark
+1  A: 

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

Francesco