Problem with itertools.groupby.

Fredrik Lundh fredrik at pythonware.com
Thu May 25 17:54:31 EDT 2006


> itertools only looks for changes to the key value (the one returned by 
> operator.itemgetter(0) in your case); it doesn't sort the list for you.
> 
> this should work:
> 
>    for k, g in itertools.groupby(sorted(vals), operator.itemgetter(0)):
>       print k, [i for i in g]

footnote: to turn the contents in an iterator into a list object, 
list(g) is a bit more convenient than [i for i in g].

</F>




More information about the Python-list mailing list