Using namedtuples field names for column indices in a list of lists

Deborah Swanson python at deborahswanson.net
Mon Jan 9 23:51:44 EST 2017


Ethan Furman wrote, on January 09, 2017 8:01 PM
> 
> 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
> >
> >--> type(records)
> > <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.

I actually have had a solid foundation of study in 2 terms of MIT's
introductory Python courses. But they can't cover everything in that
short a time.

> Working in the REPL (the python console), we can see:
> 
> Python 3.4.0 (default, Apr 11 2014, 13:05:18)
> ...
> --> type(list)
> <class 'type'>
> -->
> --> type(list())
> <class 'list'>
> --> type([1, 2, 3])
> <class 'list'>
> 
> So the `list` type is 'type', and the type of list instances 
> is 'class list'.

I just saw that while replying to MRAB. 'records' has type list, but
it's only the outer data structure that's a list. Inside, all the
records are namedtuples, and I think that accounts for behaviors that
are unlike a list of lists. (And the reason I was reluctant to accept
that it could be sorted until I tried it for myself.) The method calls I
was able to use were from the namedtuples, not the list of namedtuples.

> 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.

Yes, I think we've got that straight now.

> 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.
> 
> --
> ~Ethan~

As I mentioned, I have completed MIT's 2 introductory Python courses
with final grades of 98% and 97%.  What tutorials do you think would
significantly add to that introduction?

It's true that I didn't spend much time in the forums while I was taking
those courses, so this is the first time I've talked with people about
Python this intensively. But I'm a good learner and I'm picking up a lot
of it pretty quickly. People on the list also talk and comprehend
differently than people in the MIT courses did, so I have to become
accustomed to this as well. And the only place to learn that is right
here. 




More information about the Python-list mailing list