Is there something similar to list comprehension in dict?

Patrick Sabin patrick.just4fun at gmail.com
Fri Nov 20 04:21:40 EST 2009


Peng Yu wrote:
> I'm wondering if there is something similar to list comprehension for
> dict (please see the example code below).

Do you mean something like this:

 >>> {i:i+1 for i in [1,2,3,4]}
{1: 2, 2: 3, 3: 4, 4: 5}

This works in python3, but not in python2

- Patrick



More information about the Python-list mailing list