100

This is terribly ugly:

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

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

thenickname
  • 6,226
  • 13
  • 40
  • 42
  • 1
    I don't think it's ugly, especially if you do psData = [] # Some comment explaining what's in there – extraneon Mar 08 '10 at 16:08
  • 5
    Ugliness or not, beware with those names so similar one to each other: psData and pdata, mAData and msData... – Francesco Mar 08 '10 at 16:11
  • 1
    -1: Don't replace this with some "code golf" construction that uses the minimum number of keystrokes. This is nice. Anything more terse will baffle and frustrate folks who want to maintain this program. Indeed, anything more obscure than this will likely get rewritten to this. – S.Lott Mar 08 '10 at 19:41
  • It shouldn't be baffling if the coder includes a comment to explain what the line of code does, like # initialize list variables – CCKx Jan 20 '17 at 15:01

7 Answers7

220
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!

aerin
  • 16,939
  • 27
  • 90
  • 123
Alex Martelli
  • 811,175
  • 162
  • 1,198
  • 1,373
31
psData,nsData,msData,ckData,mAData,RData,pData = [],[],[],[],[],[],[]
YOU
  • 114,140
  • 31
  • 183
  • 213
11

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
  • 3,092
  • 1
  • 30
  • 45
  • 1
    Thanks for that! I love that python lets me write useful code as a beginner, but also has so many advanced features that I can keep learning to improve my code. – vorpal Oct 04 '17 at 03:13
  • That's one of its strong points and a testament to the ingenuity of Guido and of the other core contributors to the language design! – Francesco Oct 04 '17 at 03:27
3

A bit more efficient approach:

alist, blist, clist, dlist, elist = ([] for _ in xrange(5))

[NOTE]:

  • xrange() is more optimal than range() in Python2. (Ref)

  • The i variable was unusable so using _ is better. (Ref)

  • xrange() is no longer in Python3range() is the same with xrange().

Benyamin Jafari
  • 21,522
  • 19
  • 109
  • 128
0

Bare in mind that, tidiness may come with consequences of performance. The range function call will slow down the init process slightly. Beware if you have some process that need to reinit the variable many time.

import time
def r_init():
    st=time.time()
    alist, blist, clist, dlist, elist = ([] for i in range(5))
    et=time.time()
    print("{:.15f}".format(et-st))

def p_init():
    st=time.time()
    alist=[];blist=[];clist=[];dlist=[];elist=[]
    et=time.time()
    print("{:.15f}".format(et-st))

for x in range(1,10):
    r_init()
    p_init()
    print("\n")
mootmoot
  • 11,805
  • 5
  • 45
  • 44
-1

You can use a class to initialize/store the data, it would take more lines, but could be easier to read, and more object oriented.

Like:

class Data:
    def __init__(self):
        self.var1=[]
        <etc.>
    def zeroize(self):
        self.var1=[]
        <etc.>

Then in main near the beginning:

data=Data()

Then in your loops or anywhere in main post declaration you can use the class.

data.var1.append(varN)
if(something):
    data.zeroize()
Spectre87
  • 2,304
  • 1
  • 22
  • 36
prince
  • 1
  • this is essentially just a dict, also - it does not solve the OP's question, nor is it simpler, the OP clearly wanted less code, and less hassle, this is not that. however, this is a different type of solution to the problem, that does merit something, but it can be done much better, also, you have used some strange names. – Inbar Rose Oct 11 '12 at 13:22
-5

Something along the lines of

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

would appear to be the most elegant solution.

Bryson S.
  • 91
  • 1
  • 3