numbering variables

remi remi at non.com
Mon Mar 28 08:58:15 EST 2005


Fredrik Lundh a écrit :

>>   variable['s_i']=mylist.pop()

>     variable['s_' + str(i)]=mylist.pop()
> 
> but that while and pop stuff is pretty weird; 

Why ? Because it destroys the list ?

>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

maybe a dictionnary is more easely usable and reusable than a list...
Thanks a lot for your help.

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

As a beginner, I agree at 100% even if the latter is nicer !
Rémi.
I send to people form clp a little sun from Toulouse (France)



More information about the Python-list mailing list