Patrick Sabin <[email protected]> writes: > 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
Of course in python 2 you can do:
>>> dict((i, i+1) for i in [1,2,3,4])
{1: 2, 2: 3, 3: 4, 4: 5}
--
http://mail.python.org/mailman/listinfo/python-list
