Comprehension consistency at last in Python 3.0!

Comprehension consistency at last in Python 3.0!.

Nice example of how comprehensions in Python 3.0 seem a lot more intuitive.

Earlier in Python 2.5 :

Python 2.5.2 (r252:60911, Nov 14 2008, 19:46:32)
[GCC 4.3.2] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>>
>>> dict((n, n*n) for n in range(5))
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

New in Python 3.0

Python 3.0 (r30:67503, Dec  4 2008, 10:23:44)
[GCC 4.3.2] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>>
>>> [ n*n for n in range(5) ]
[0, 1, 4, 9, 16]
>>>
>>> { n*n for n in range(5) }
{0, 1, 4, 16, 9}
>>>
>>> { n: n*n for n in range(5) }
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
Posted on December 5, 2008 at 10:59 am by Dhananjay Nene · Permalink
In: Interesting · Tagged with: ,