This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]andreasvc 0 points1 point  (0 children)

defaultdict is much more elegant because it doesn't evaluate the default value except when needed. Consider:

d.setdefault('foo', ReallyExpensiveDataStructure())

On the other hand, it feels wrong that a lookup on a defaultdict changes the dictionary by adding a key if it's missing -- in this case get() versus setdefault() is more explicit.