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

Erik python at
Mon Jan 9 23:05:42 EST 2017

On 10/01/17 03:02, Deborah Swanson wrote:
> Erik wrote, on January 09, 2017 5:47 PM
>> 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)

No, the list is _extended_ by that code. The list is _created_ with a 
line that will say something like "records = []" or "records = list()" 
(or "records = <expression that yields a list>").

It's nice to see you agree that it's a list though. Oh, hold on ... ;)

> I'm not exactly
> sure if this statement is a list comprehension.

No, it's not. I was remembering an old message where someone suggested 
using the _make() method and that was expressed as a list comprehension.

What you have there is a call to list.extend() passing a _generator_ 
comprehension as its only parameter (which in this case you can consider 
to be equivalent to a list comprehension as all of the data are 
generated greedily). You see that I said "list.extend()". That's because 
'records' is a list.

>>>> type(records)
> <class 'list'>

Yes, it's an instance of the list class. A list object. A list.

 >>> type(list())
<class 'list'>
 >>> type([])
<class 'list'>
 >>> class foo: pass
 >>> type(foo())
<class ''>

... type() will tell you what class your object is an instance of. 
"<class 'list'>" tells you that your object is a list.

> And it behaves like a list sometimes, but many times
> not.

I think that's impossible. I'm 100% sure it's a list. Please give an 
example of when 'records' does not behave like a list.

> The only thing I don't think you have 100% correct is your assertion
> that records is a list.

It's a list.

> But that's just a quibble. The important thing in this context is that
> both .sort() and sorted() treat it like a list and DTRT.

That's because it's a list :)


More information about the Python-list mailing list