defaultdict and dict?

Hrvoje Niksic hniksic at
Fri Nov 9 12:24:05 CET 2007

Davy <zhushenli at> writes:

> In Python 2.5 document, defaultdict is called high performance
> container. From document, we can know defaultdict is subclass of dict.
> So, why defaultdict have higher performance than dict?

I don't think the "high performance" attribute is supposed to mean
that it has higher performanec than dict, only that it is not a
simple-minded hack on top of dict, but code carefully written in C
with performance in mind -- like dict itself.

> That is, when use defaultdict and when use dict? Any suggestions are
> welcome!

Use defaultdict when you need the dict to handle missing keys
automatically, dict in all other cases.  If in doubt, simply use dict.

More information about the Python-list mailing list