groupby

Paul McGuire ptmcg at austin.rr._bogus_.com
Tue May 23 02:01:43 EDT 2006


"Bryan" <belred at gmail.com> wrote in message
news:mailman.6100.1148362738.27775.python-list at python.org...
> George Sakkis wrote:
<snip>
> > "The returned group is itself an iterator that shares the underlying
> > iterable with groupby(). Because the source is shared, when the groupby
> > object is advanced, the previous group is no longer visible. So, if
> > that data is needed later, it should be stored as a list"
> >
> > George
> >
>
> i read that description in the docs so many times before i posted here.
now that
> i read it about 10 more times, i finally get it.  there's just something
about
> the wording that kept tripping me up, but i can't explain why :)
>
> thanks,
>
> bryan
>

So here's how to save the values from the iterators while iterating over the
groupby:

>>> m = [(x,list(y)) for x,y in groupby([1, 1, 1, 2, 2, 3])]
>>> m
[(1, [1, 1, 1]), (2, [2, 2]), (3, [3])]

-- Paul





More information about the Python-list mailing list