[Python-Dev] defaultdict and on_missing()

Edward C. Jones edcjones at comcast.net
Wed Feb 22 21:27:56 CET 2006


Guido van Rossen wrote:
> I think the pattern hasn't been commonly known; people have been
> struggling with setdefault() all these years.

I use setdefault _only_ to speed up the following code pattern:

if akey not in somedict:
     somedict[akey] = list()
somedict[akey].append(avalue)

These lines of simple Python are much easier to read and write than

somedict.setdefault(akey, list()).append(avalue)


More information about the Python-Dev mailing list