IndexError in simple list processing function - what am I missing?
<p>This is one of those embarassing pieces of code that simply needs a second pair of eyes to spot the "obvious" mistake. I am processing a list of lists, and for some reason, I am hitting a 'List Index out of range' erorr - and I can't spot it despite peering at the screen for a while.</p>

<p>Here is the snippet:</p>

<pre><code>def group_ldata(data, freq, normal_grouping = True):
    if freq &gt; 1 and len(data) &gt; (SOME_SANITY_FACTOR*freq):
        i, output, subset, lastpos = (0, [],[], len(data)-freq)
        if not normal_grouping:
            for i in range(lastpos):
                pass
        else:
            while True:
                subset = data[i:freq]
                #print subset
                firstrow = subset[0]
                lastrow = subset[-1]
                output.append((firstrow[0], firstrow[1], lastrow[2]))
                i += freq
                if i &gt;= lastpos:
                    break
        return output
    else:
        return data
</code></pre>

<p>Here's some sample data to boot:</p>

<pre><code>&gt;&gt;&gt; a =[]
&gt;&gt;&gt; a.append(range(0,5))
&gt;&gt;&gt; a.append(range(5,10))
&gt;&gt;&gt; a.append(range(10,15))
&gt;&gt;&gt; a.append(range(15,20))
&gt;&gt;&gt; a.append(range(20,25))
&gt;&gt;&gt; a.append(range(25,30))
&gt;&gt;&gt; a.append(range(30,35))
</code></pre>

<p>Here is the result when I run it:</p>

<pre><code>&gt;&gt;&gt; b = group_ldata(a,2)
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
[]
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "&lt;stdin&gt;", line 11, in group_ldata
IndexError: list index out of range
</code></pre>

<p>Can anyone spot what is causing the error?</p>
