[Python-Dev] map(None, ...) in tutorial

Raymond Hettinger python at rcn.com
Thu Aug 14 18:13:31 EDT 2003


> I've noticed this section in the tutorial:
> 
>     [...], we see that map(None, list1, list2) is a convenient way of
>     turning a pair of lists into a list of pairs.  For example:
> 
>     >>> map(None, seq, map(square, seq))
>     [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)]
> 
> I think the example be changed to use zip() instead, ie:
> 
>     >>> zip(seq, map(square, seq))
>     [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)]
> 
> Any objections?


No objections, just a couple of thoughts:

* [(x, square(x) for x in seq]  # this is much nicer

* map(None, 'abc', range(5))  # demonstrates None fill-in 
                                              # (which zip() doesn't have).

* to unzip the above zip example:   zip(*result)
   # turns the list of pairs back into a pair of lists


Raymond Hettinger



More information about the Python-Dev mailing list