Preferred Python idiom for handling non-existing dictionary keys and why?

Terry Reedy tjreedy at udel.edu
Fri Oct 10 15:41:52 EDT 2003


"Quentin Crain" <czrpb at yahoo.com> wrote in message
news:mailman.1065807808.957.python-list at python.org...
>
> Hello again All!
>
> (First, I would like to mention I did try to google
> for the answer here!)

The question you asked is frequent, but hard to isolate, and has
special-case answer (to question you did not ask) even harder to find.

> Say I am populating a dictionary with a list and
> appending. I have written it thusly:
>
>   d={}
>   for (k,v) in somedata():
>       try:
>           d[k].append(v)
>       except KeyError:
>           d[k]=[v]
>
> I could have written:
>
>   d={}
>   for (k,v) in somedata():
>       if (k in d):
>           d[k].append(v)
>       else:
>           d[k]=[v]
>
>
> Which is perferred and why?

Neither.  For me, both are superceded by

d={}
for (k,v) in somedata():
    d[k] = d.get(k, []).append(v)

Read Library Reference 2.2.7 Mapping Types to learn current dict
methods.

> Which is "faster"?

Tradeoff is small extra overhead every loop (the condition) versus
'occasional' big overhead (exception catching).  Choice depends on
frequency of exceptions.  As I remember, one data-based rule of thumb
from years ago is to use conditional if frequency more that 10%.  You
could try new timeit() on all three versions.

Terry J. Reedy






More information about the Python-list mailing list