Is there something similar to list comprehension in dict?

Tim Golden mail at timgolden.me.uk
Fri Nov 20 06:47:26 EST 2009


Michele Simionato wrote:
> On Nov 20, 4:18 am, Peng Yu <pengyu... at gmail.com> wrote:
>> I'm wondering if there is something similar to list comprehension for
>> dict
> 
> Yes, but only in Python 3:
> 
>>>> {(i, x) for i, x in enumerate('abc')}
> {(0, 'a'), (1, 'b'), (2, 'c')}

Although the 2.x syntax is hardly onerous:

dict ((i+5, x) for i, x in enumerate ('abc'))


-- obviously without something like the i+5, the example
equates to dict (enumerate ('abc')) 

:)

TJG



More information about the Python-list mailing list