Namedtuples: some unexpected inconveniences

Deborah Swanson python at deborahswanson.net
Wed Apr 12 17:17:12 EDT 2017



> -----Original Message-----
> From: Python-list 
> [mailto:python-list-bounces+python=deborahswanson.net at python.o
> rg] On Behalf Of MRAB
> Sent: Wednesday, April 12, 2017 1:42 PM
> To: python-list at python.org
> Subject: Re: Namedtuples: some unexpected inconveniences
> 
> 
> On 2017-04-12 20:57, Deborah Swanson wrote:
> > Are these bugs, or was there something I could have done to avoid 
> > these problems? Or are they just things you need to know 
> working with namedtuples?
> > 
> > The list of namedtuples was created with:
> > 
> > infile = open("E:\\Coding projects\\Pycharm\\Moving\\Moving 
> 2017 in -
> > test.csv")
> > rows = csv.reader(infile)fieldnames = next(rows)
> > Record = namedtuple("Record", fieldnames)
> > records = [Record._make(fieldnames)]
> > records.extend(Record._make(row) for row in rows)
> >      . . .
> > (many lines of field processing code)
> >      . . .
> > 
> > then the attempt to group the records by title:
> > 
> > import operator
> > records[1:] = sorted(records[1:], key=operator.attrgetter("title",
> > "Date")) groups = defaultdict() for r in records[1:]:
> >      # if the key doesn't exist, make a new group
> >      if r.title not in groups.keys():
> >          groups[r.title] = [r]
> >      # if key (group) exists, append this record
> >      else:
> >          groups[r.title].append(r)
> > 
> > (Please note that this default dict will not automatically make new 
> > keys when they are encountered, possibly because the keys of the 
> > defaultdict are made from namedtuples and the values are 
> namedtuples. 
> > So you have to include the step to make a new key when a key is not 
> > found.)

MRAB said:
 
> The defaultdict _will_ work when you use it properly. :-)
> 
> The line should be:
> 
>      groups = defaultdict(list)
> 
> so that it'll make a new list every time a new key is 
> automatically added.

Arg. Now I remember the thought crossing my mind early on, and noticing
that the characterizing property of a defaultdict was what you set the
default to be. Too bad I forgot that useful thought once I was entangled
with all those other problems.

Thanks for jogging that memory stuck in a hidey hole.

> Another point: namedtuples, as with normal tuples, are immutable; once

> created, you can't change an attribute. A dict might be a better bet.

Yes, namedtuples still being tuples was a point mentioned in passing by
someone, I think Steve D'Aprano, but I didn't immediately see that as
being the roadblock to accessing field values by variable. It does make
sense now though, although others on the list also didn't see it, so I'm
not feeling as bad about it as I could.

Namedtuples absolutely was the right data structure for two thirds of
this program. I only ran into trouble with it trying to do the
defaultdict group by thing. And it all turned out ok just by going back
to the original list.

Now, if I could understand why the namedtuples grouped by the
defaultdict were only copied instead of remaining the same namedtuples
as the list they were copied from, that should wrap this set of problems
up.

Many thanks again!

Deborah




More information about the Python-list mailing list