# itertools examples

Discussion in 'Python' started by Felipe Almeida Lessa, Feb 11, 2006.

1. ### Felipe Almeida LessaGuest

Hi,

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]

?

AFAIK, list comprehensions are more preferable than map's as they're clearer. Or am I wrong?

Cya,
Felipe.

--
"Quem excele em empregar a forÃ§a militar subjulga os exÃ©rcitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrÃ³i os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o CÃ©u com o propÃ³sito primordial da
'preservaÃ§Ã£o'. Desse modo suas armas nÃ£o se embotarÃ£o, e os ganhos
poderÃ£o ser preservados. Essa Ã© a estratÃ©gia para planejar ofensivas."

-- Sun Tzu, em "A arte da guerra"

Felipe Almeida Lessa, Feb 11, 2006

2. ### Raymond HettingerGuest

[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

Raymond Hettinger, Feb 12, 2006

3. ### Felipe Almeida LessaGuest

Em SÃ¡b, 2006-02-11 Ã s 20:16 -0800, Raymond Hettinger escreveu:
> 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.

I know both work in the same way, but IIRC I heard someone (GvR?) saying
list comprehensions should be used when possible to substitute map,
filter and/or reduce.

> 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.

IMO at a first glance the it's much easier to read and understand the
list comprehension, but I have to admit that if I didn't see the
operator.itemgetter(1) there I would probably never known it existed.
Well, so let's just leave it there, but I'll surely program with the
list comprehensions ;-).

Thanks for your attention,
Felipe.

>
> Raymond
>

--
"Quem excele em empregar a forÃ§a militar subjulga os exÃ©rcitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrÃ³i os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o CÃ©u com o propÃ³sito primordial da
'preservaÃ§Ã£o'. Desse modo suas armas nÃ£o se embotarÃ£o, e os ganhos
poderÃ£o ser preservados. Essa Ã© a estratÃ©gia para planejar ofensivas."

-- Sun Tzu, em "A arte da guerra"

Felipe Almeida Lessa, Feb 12, 2006