itertools examples

Raymond Hettinger python at rcn.com
Sat Feb 11 23:16:15 EST 2006


[Felipe Almeida Lessa]
> IMHO, on http://www.python.org/doc/current/lib/itertools-example.html ,
> shouldn't the part
>
> >>> for k, g in groupby(enumerate(data), lambda (i,x):i-x):
> ...     print map(operator.itemgetter(1), g)
>
> be
>
> >>> for k, g in groupby(enumerate(data), lambda (i, x): i-x):
> ...     print [i[1] for i in g]

Both work just fine.  It's a personal choice when to use map() and when
to use a list comprehension.  Since many itertools have the flavor of
map/filter, its use is not out of place in the itertools docs.

Also, the use of map() provided an opportunity to demonstrate
operator.itemgetter().  While not essential to this example, it is
helpful with several other tools (especially those with a key=
argument).  Itertools provide a kind of iterator algebra and
itemgetter() is an essential part of that algebra; hence, it is
appropriate that it be included in itertool examples.

If your taste says otherwise, that's okay.  Program however you want.
If reading the examples helped you understand the toolset, then the
docs accomplished their goal.


Raymond




More information about the Python-list mailing list