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

Peter Otten __peter__ at
Sun Jan 8 06:01:23 EST 2017

Deborah Swanson wrote:

> to do that is with .fget(). Believe me, I tried every possible way to
> use instance.A or instance[1] and no way could I get ls[instance.A].

Sorry, no.

To get a list of namedtuple instances use:

rows = csv.reader(infile)
Record = namedtuple("Record", next(rows))
records = [Record._make(row) for row in rows]

If you want a column from a list of records you need to extract it manually:

columnA = [record.A for record in records]

More information about the Python-list mailing list