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

Deborah Swanson python at deborahswanson.net
Sun Jan 8 15:22:52 EST 2017


Paul Rudin wrote, on January 08, 2017 6:49 AM
> 
> "Deborah Swanson" <python at deborahswanson.net> writes:
> 
> > Peter Otten wrote, on January 08, 2017 3:01 AM
> >> 
> >> columnA = [record.A for record in records]
> >
> > This is very neat. Something like a list comprehension for named 
> > tuples?
> 
> Not something like - this *is* a list comprehension - it 
> creates a list of named tuples.
> 
> The thing you iterate over within the comprehension can be 
> any iterator. (Of course you're going to run into problems if 
> you try to construct a list from an infinite iterator.)

Thanks Paul. I've been meaning to spend some time getting to thoroughly
know list comprehensions for awhile now, but I keep running into so many
new things I just haven't gotten to it. I thought it looked like one,
but I hedged my wording because I wasn't sure.  Infinite iterators
definitely sound like something to remember!



More information about the Python-list mailing list