Python - Initializing Multiple Lists/Line

PythonListInitialization

Python Problem Overview


This is terribly ugly:

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

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

Python Solutions


Solution 1 - Python

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!

Solution 2 - Python

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

Solution 3 - Python

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

Solution 4 - Python

A bit more efficient approach:

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

[NOTE]:

Solution 5 - Python

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")

Solution 6 - Python

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()

Solution 7 - Python

Something along the lines of

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

would appear to be the most elegant solution.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionthenicknameView Question on Stackoverflow
Solution 1 - PythonAlex MartelliView Answer on Stackoverflow
Solution 2 - PythonYOUView Answer on Stackoverflow
Solution 3 - PythonFrancescoView Answer on Stackoverflow
Solution 4 - PythonBenyamin JafariView Answer on Stackoverflow
Solution 5 - PythonmootmootView Answer on Stackoverflow
Solution 6 - PythonprinceView Answer on Stackoverflow
Solution 7 - PythonBryson S.View Answer on Stackoverflow