How do I use Python's itertools.groupby()?

Posted by James Sulak on Stack Overflow See other posts from Stack Overflow or by James Sulak
Published on 2008-08-03T18:27:09Z Indexed on 2010/04/07 10:33 UTC
Read the original article Hit count: 387

Filed under:
|

I haven't been able to find an understandable explanation of how to actually use Python's itertools.groupby() function. What I'm trying to do is this: take a list - in this case, the children of an objectified lxml element - divide it into groups based on some criteria, and then later iterate over each of these groups separately.

I've reviewed the documentation (http://docs.python.org/lib/itertools-functions.html), and the examples, (http://docs.python.org/lib/itertools-example.html), but I've had trouble trying to apply them beyond a simple list of numbers.

So, how do I use of itertools.groupby()? Is there another technique I should be using? Pointers to good "prerequisite" reading would also be appreciated.

© Stack Overflow or respective owner

Related posts about python

Related posts about iteration