list 2 dict?

MRAB python at mrabarnett.plus.com
Tue Jan 4 13:29:53 EST 2011


On 04/01/2011 05:33, Paul Rubin wrote:
> "Octavian Rasnita"<orasnita at gmail.com>  writes:
>> If I want to create a dictionary from a list...
>> l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
>
>      dict(l[i:i+2] for i in xrange(0,len(l),2))
>
> seems simplest to me.

Or:

     dict(zip(l[0 : : 2], l[1 : : 2]))



More information about the Python-list mailing list