Using namedtuples field names for column indices in a list of lists
ethan at stoneleaf.us
Mon Jan 9 23:00:47 EST 2017
On 01/09/2017 07:02 PM, Deborah Swanson wrote:
> Erik wrote, on January 09, 2017 5:47 PM
>> As people keep saying, the object you have called 'records'
>> is a *list*
>> of namedtuple objects. It is not a namedtuple.
>> IIRC, you create it using a list comprehension which creates the
>> records. A list comprehension always creates a list.
> Well no. The list is created with:
> records.extend(Record._make(row) for row in rows)
> I'm new to both namedtuples and list comprehensions, so I'm not exactly
> sure if this statement is a list comprehension. It looks like it could
> be. In any case I recreated records in IDLE and got
> <class 'list'>
> So it's a class, derived from list? (Not sure what the 'list' means.)
On the one hand, Deborah, I applaud your perseverance. On the other, it seems as if you trying to run before you can walk. I know tutorials can be boring, but you really should go through one so you have a basic understanding of the fundamentals.
Working in the REPL (the python console), we can see:
Python 3.4.0 (default, Apr 11 2014, 13:05:18)
--> type([1, 2, 3])
So the `list` type is 'type', and the type of list instances is 'class list'.
Your records variable is an instance of a list filled with instances of a namedtuple, 'Record'. One cannot sort a namedtuple, but one can sort a list of namedtuples -- which is what you are doing.
As I said earlier, I admire your persistence -- but take some time and learn the basic vocabulary as that will make it much easier for you to ask questions, and for us to give you meaningful answers.
More information about the Python-list