numbering variables
Fredrik Lundh
fredrik at pythonware.com
Mon Mar 28 08:13:51 EST 2005
"remi" <remi at non.com> wrote:
> i = 0
> while i<=len(mylist)
> variable['s_i']=mylist.pop()
> i = i+1
variable['s_' + str(i)]=mylist.pop()
but that while and pop stuff is pretty weird; maybe you should read the
sections on lists, for loops, and string formatting in the Python tutorial?
here's a shorter way to create that dictionary:
variable = {}
for item in mylist:
variable["s_%d" % len(variable)] = item
and here's the obligatory one-liner:
variable = dict(("s_" + str(k), v) for k, v in enumerate(mylist))
(shorter, but not exactly clearer, especially not if you're new to Python)
</F>
More information about the Python-list
mailing list