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

Deborah Swanson python at deborahswanson.net
Mon Jan 9 19:54:06 EST 2017


Peter Otten wrote, on January 09, 2017 3:27 PM
> 
> While stable sort is nice in this case you can just say
> 
> key=operator.attrgetter("Description", "Date")
> 
> Personally I'd only use sorted() once and then switch to the 
> sort() method.

This works perfectly, thank you.

As I read the docs, the main (only?) difference between sorted() and
.sort() is that .sort() sorts the list in place.

Since I won't change the order of the records again after the sort, I'm
using 

records.sort(key=operator.attrgetter("Description", "Date"))

once, which also works perfectly.

So both sorted() and sort() can be used to sort namedtuples.  Good to
know!




More information about the Python-list mailing list