Namedtuples problem

Ben Bacarisse ben.usenet at bsb.me.uk
Thu Feb 23 19:23:48 EST 2017


"Deborah Swanson" <python at deborahswanson.net> writes:

>> -----Original Message-----
>> From: Erik [mailto:python at lucidity.plus.com] 
>> Sent: Thursday, February 23, 2017 2:09 AM
>> To: python at deborahswanson.net; python-list at python.org
>> Subject: Re: Namedtuples problem
>> 
>> 
>> Hi,
>> 
>> On 23/02/17 09:38, Deborah Swanson wrote:
>> > group[[idx][records_idx[label]]]
>> > gets an IndexError: 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.
>
> Actually, group is essentially a 2-element list. Each group has a list
> of rows, and each row has a set of fields. group has to be indexed by
> row index and field index. (This is a namedtuple configuration.)
>
> The weirdness is that 
>
> group[0][4]

Sure.  No trouble there.

> gets the right answer, but
>
> group[[idx][records_idx[label]]], 
> where idx = 0 and records_idx[label]] = 4
>
> gets the IndexError.

group is not involved in the index error.  Just write

  [idx][records_idx[label]]

and you'll get the error.  Wrapping that up in group[...] won't make a
difference.  In fact, since idx is 0 and records_idx[label]] is 4, you
get the same error (for the same reason) by typing

  [0][4]

into a Python REPL.  Does it still seem weird?

This is just another shot at explaining the issue because, sometimes,
different words can help.  I thought the message you are replying to (as
well as others) explained it well, but it obviously it did not hit the
spot.

<snip>
-- 
Ben.



More information about the Python-list mailing list