[Python-Dev] dict.addlist()

Barry Warsaw barry at python.org
Tue Jan 20 09:52:05 EST 2004

On Tue, 2004-01-20 at 09:26, Bob Ippolito wrote:
> On Jan 20, 2004, at 9:17 AM, Raymond Hettinger wrote:
> > Here is anidea to kick around:
> >
> > Evolve and eventually replace dict.setdefault with a more specialized
> > method that is clearer, cleaner, easier to use, and faster.
> >
> >        d.addlist(k, v)   would work like   d.setdefault(k, 
> > []).append(v)
> -1
> There are other reasons to use setdefault.  This one is pretty common 
> though, but I think a more generic solution could be implemented.
> Perhaps:
> d.setdefault(k, factory=list).append(v) ?

d.setdefault(k, []).append(v)

I'm not sure what any of the other suggestions buy you except avoiding a
list instantiation, which doesn't seem like enough to warrant the extra
complexity.  I use setdefault() quite a bit (big surprise, huh?) and
occasionally would like lazy evaluation of the second argument, but it
usually doesn't bother me.


More information about the Python-Dev mailing list