Namedtuples problem

Erik python at lucidity.plus.com
Thu Feb 23 05:08:38 EST 2017


Hi,

On 23/02/17 09:38, Deborah Swanson wrote:
> group[[idx][records_idx[label]]]
> gets an Index Error: list index out of range

[snip]

> Can anyone see why I'm getting this Index error? and how to fix it?

It looks to me like you are indexing into a single-element list that you 
are creating using the literal list syntax in the middle of the expression.

If we were to break the expression into parts to make it a bit simpler 
to refer to discuss:

ridx = records_idx[label]
group[[idx][ridx]]

You can now more easily see that 'group' is being indexed by the 
expression "[idx][ridx]". What does that mean?

[idx] is creating a single-element list using literal list syntax. This 
is then indexed using 'ridx' (using, perhaps confusingly, the exact same 
syntax to do a different thing).

The result of *that* expression is then being used to index 'group', but 
it won't get that far because you'll get the exception if 'ridx' is 
anything but zero.

So the initial problem at least is the extra [] around 'idx' which is 
creating a list on the fly for you.

E.




More information about the Python-list mailing list