============= defaultdict ============= The standard dictionary includes the method :func:`setdefault` for retrieving a value and establishing a default if the value does not exist. By contrast, :class:`defaultdict` lets the caller specify the default up front when the container is initialized. .. include:: collections_defaultdict.py :literal: :start-after: #end_pymotw_header This works well as long as it is appropriate for all keys to have the same default. It can be especially useful if the default is a type used for aggregating or accumulating values, such as a :class:`list`, :class:`set`, or even :class:`int`. The standard library documentation includes several examples of using :class:`defaultdict` this way. .. {{{cog .. cog.out(run_script(cog.inFile, 'collections_defaultdict.py')) .. }}} :: $ python collections_defaultdict.py d: defaultdict(, {'foo': 'bar'}) foo => bar bar => default value .. {{{end}}} .. seealso:: `defaultdict examples `_ Examples of using defaultdict from the standard library documentation. `James Tauber: Evolution of Default Dictionaries in Python `_ Discussion of how defaultdict relates to other means of initializing dictionaries.