How do I use Python's itertools.groupby()?
<p>I haven't been able to find an understandable explanation of how to actually use Python's <code>itertools.groupby()</code> function.  What I'm trying to do is this:</p>

<ul>
<li>Take a list - in this case, the children of an objectified <code>lxml</code> element</li>
<li>Divide it into groups based on some criteria</li>
<li>Then later iterate over each of these groups separately.</li>
</ul>

<p>I've reviewed <a href="https://docs.python.org/2/library/itertools.html">the documentation</a>, and <a href="https://docs.python.org/2/library/itertools.html#itertools.groupby">the examples</a>, but I've had trouble trying to apply them beyond a simple list of numbers. </p>

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

<p>As Sebastjan said, <strong>you first have to sort your data. This is important.</strong></p>

<p>The part I didn't get is that in the example construction</p>

<pre><code>groups = []
uniquekeys = []
for k, g in groupby(data, keyfunc):
   groups.append(list(g))    # Store group iterator as a list
   uniquekeys.append(k)
</code></pre>

<p><code>k</code> is the current grouping key, and <code>g</code> is an iterator that you can use to iterate over the group defined by that grouping key. In other words, the <code>groupby</code> iterator itself returns iterators.</p>

<p>Here's an example of that, using clearer variable names:</p>

<pre><code>from itertools import groupby

things = [("animal", "bear"), ("animal", "duck"), ("plant", "cactus"), ("vehicle", "speed boat"), ("vehicle", "school bus")]

for key, group in groupby(things, lambda x: x[0]):
    for thing in group:
        print "A %s is a %s." % (thing[1], key)
    print " "
</code></pre>

<p>This will give you the output:</p>

<blockquote>
  <p>A bear is a animal.<br>
  A duck is a animal.</p>
  
  <p>A cactus is a plant.</p>
  
  <p>A speed boat is a vehicle.<br>
  A school bus is a vehicle.</p>
</blockquote>

<p>In this example, <code>things</code> is a list of tuples where the first item in each tuple is the group the second item belongs to. </p>

<p>The <code>groupby()</code> function takes two arguments: (1) the data to group and (2) the function to group it with. </p>

<p>Here, <code>lambda x: x[0]</code> tells <code>groupby()</code> to use the first item in each tuple as the grouping key.</p>

<p>In the above <code>for</code> statement, <code>groupby</code> returns three (key, group iterator) pairs - once for each unique key. You can use the returned iterator to iterate over each individual item in that group.</p>

<p>Here's a slightly different example with the same data, using a list comprehension:</p>

<pre><code>for key, group in groupby(things, lambda x: x[0]):
    listOfThings = " and ".join([thing[1] for thing in group])
    print key + "s:  " + listOfThings + "."
</code></pre>

<p>This will give you the output:</p>

<blockquote>
  <p>animals: bear and duck.<br>
  plants: cactus.<br>
  vehicles: speed boat and school bus.</p>
</blockquote>
