list 2 dict?
Stefan Sonnenberg-Carstens
stefan.sonnenberg at pythonmeister.com
Sun Jan 2 10:15:41 EST 2011
Am 02.01.2011 14:18, schrieb Octavian Rasnita:
> Hi,
>> If I want to create a dictionary from a list, is there a better way than the long line below?
>> l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
>> d = dict(zip([l[x] for x in range(len(l)) if x %2 == 0], [l[x] for x in range(len(l)) if x %2 == 1]))
>> print(d)
>> {8: 'b', 1: 2, 3: 4, 5: 6, 7: 'a'}
>> Thanks.
>> Octavian
>Or so:
dict(zip((y for x,y in enumerate(l) if x%2==0),(y for x,y in
enumerate(l) if not x%2==0)))
More information about the Python-list
mailing list