list 2 dict?

Stefan Sonnenberg-Carstens stefan.sonnenberg at pythonmeister.com
Sun Jan 2 13:42:35 EST 2011


Am 02.01.2011 19:19, schrieb Emile van Sebille:
> On 1/2/2011 8:31 AM Stefan Sonnenberg-Carstens said...
>
>> A last one:
>>
>> l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
>> dict((x[1],x[0]) for x in ((l.pop(),l.pop()) for x in xrange(len(l)/2)))
>
> This also works:
>
>
> l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
>
> pop=l.pop
>
> dict([(pop(),pop()) for i in l])
>
No, it does not:

 >>> l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
 >>>
 >>> pop=l.pop
 >>>
 >>> dict([(pop(),pop()) for i in l])
{'a': 7, 'b': 8, 4: 3, 6: 5}
 >>>



More information about the Python-list mailing list