[Tutor] Flatten a list in tuples and remove doubles

eryksun eryksun at gmail.com
Sun Jul 29 08:53:54 CEST 2012


On Sat, Jul 28, 2012 at 7:12 PM, Francesco Loffredo <fal at libero.it> wrote:
>
> My bad, now I'll RTFM again and I will study very carefully the operator and
> itertools modules.

I forgot to mention a gotcha about groupby's implementation. The
grouby object and the yielded _grouper objects share a single
iterator. Here's a (somewhat contrived) example of a mistake:

>>> groups = groupby(sorted(data, key=keyfunc), keyfunc)
>>> groups = list(groups)  #DON'T DO THIS
>>> groups
[((0, '3eA', 'Dupont', 'Juliette'), <itertools._grouper object at 0xb74734ec>),
 ((1, '3eA', 'Pop', 'Iggy'), <itertools._grouper object at 0xb74735cc>)]

>>> list(groups[0][1])  #EMPTY
[]
>>> list(groups[1][1])  #ONLY THE LAST ITEM
[(1, '3eA', 'Pop', 'Iggy', 5, 40.5, 60.0)]


More information about the Tutor mailing list