This is terribly ugly:
psData = []
nsData = []
msData = []
ckData = []
mAData = []
RData = []
pData = []
Is there a way to declare these variables on a single line?
asked Mar 8, 2010 at 16:03
thenicknamethenickname
6,39414 gold badges40 silver badges42 bronze badges
4
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
18.2k28 gold badges93 silver badges130 bronze badges
answered Mar 8, 2010 at 16:04
Alex MartelliAlex Martelli
824k163 gold badges1203 silver badges1380 bronze badges
5
psData,nsData,msData,ckData,mAData,RData,pData = [],[],[],[],[],[],[]
answered Mar 8, 2010 at 16:05
1
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: //docs.python.org/library/collections.html#collections.defaultdict
answered Mar 8, 2010 at 16:07
FrancescoFrancesco
3,1101 gold badge31 silver badges45 bronze badges
2
A bit more efficient approach:
alist, blist, clist, dlist, elist = [[] for _ in xrange[5]]
[NOTE]:
xrange[]
is more optimal thanrange[]
in Python2. [Ref]The
i
variable was unusable so using_
is better. [Ref]xrange[]
is no longer in Python3 —range[]
is the same withxrange[]
.
answered Oct 20, 2018 at 7:28
Benyamin JafariBenyamin Jafari
23.7k21 gold badges113 silver badges135 bronze badges
1
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"]
answered Jan 13, 2017 at 11:05
mootmootmootmoot
12.3k5 gold badges46 silver badges44 bronze badges
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=[]
def zeroize[self]:
self.var1=[]
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,3441 gold badge22 silver badges36 bronze badges
answered Jul 11, 2012 at 17:12
1
Something along the lines of
alist, blist, clist, dlist, elist = [[],]*5
would appear to be the most elegant solution.
answered Sep 14, 2018 at 16:31
Bryson S.Bryson S.
911 silver badge3 bronze badges
1