
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. -Barry