<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>defaultdict — Python Module of the Week</title> <link rel="stylesheet" href="../_static/sphinxdoc.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '1.132', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <link rel="author" title="About these documents" href="../about.html" /> <link rel="top" title="Python Module of the Week" href="../index.html" /> <link rel="up" title="collections – Container data types" href="index.html" /> <link rel="next" title="Deque" href="deque.html" /> <link rel="prev" title="Counter" href="counter.html" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="deque.html" title="Deque" accesskey="N">next</a> |</li> <li class="right" > <a href="counter.html" title="Counter" accesskey="P">previous</a> |</li> <li><a href="../contents.html">PyMOTW</a> »</li> <li><a href="../data_types.html" >Data Types</a> »</li> <li><a href="index.html" accesskey="U">collections – Container data types</a> »</li> </ul> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="counter.html" title="previous chapter">Counter</a></p> <h4>Next topic</h4> <p class="topless"><a href="deque.html" title="next chapter">Deque</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/collections/defaultdict.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" size="18" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="defaultdict"> <h1>defaultdict<a class="headerlink" href="#defaultdict" title="Permalink to this headline">¶</a></h1> <p>The standard dictionary includes the method <tt class="xref py py-func docutils literal"><span class="pre">setdefault()</span></tt> for retrieving a value and establishing a default if the value does not exist. By contrast, <tt class="xref py py-class docutils literal"><span class="pre">defaultdict</span></tt> lets the caller specify the default up front when the container is initialized.</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">collections</span> <span class="k">def</span> <span class="nf">default_factory</span><span class="p">():</span> <span class="k">return</span> <span class="s">'default value'</span> <span class="n">d</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">defaultdict</span><span class="p">(</span><span class="n">default_factory</span><span class="p">,</span> <span class="n">foo</span><span class="o">=</span><span class="s">'bar'</span><span class="p">)</span> <span class="k">print</span> <span class="s">'d:'</span><span class="p">,</span> <span class="n">d</span> <span class="k">print</span> <span class="s">'foo =>'</span><span class="p">,</span> <span class="n">d</span><span class="p">[</span><span class="s">'foo'</span><span class="p">]</span> <span class="k">print</span> <span class="s">'bar =>'</span><span class="p">,</span> <span class="n">d</span><span class="p">[</span><span class="s">'bar'</span><span class="p">]</span> </pre></div> </div> <p>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 <tt class="xref py py-class docutils literal"><span class="pre">list</span></tt>, <tt class="xref py py-class docutils literal"><span class="pre">set</span></tt>, or even <tt class="xref py py-class docutils literal"><span class="pre">int</span></tt>. The standard library documentation includes several examples of using <tt class="xref py py-class docutils literal"><span class="pre">defaultdict</span></tt> this way.</p> <div class="highlight-python"><pre>$ python collections_defaultdict.py d: defaultdict(<function default_factory at 0x10045ba28>, {'foo': 'bar'}) foo => bar bar => default value</pre> </div> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt><a class="reference external" href="http://docs.python.org/lib/defaultdict-examples.html">defaultdict examples</a></dt> <dd>Examples of using defaultdict from the standard library documentation.</dd> <dt><a class="reference external" href="http://jtauber.com/blog/2008/02/27/evolution_of_default_dictionaries_in_python/">James Tauber: Evolution of Default Dictionaries in Python</a></dt> <dd>Discussion of how defaultdict relates to other means of initializing dictionaries.</dd> </dl> </div> </div> </div> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="deque.html" title="Deque" >next</a> |</li> <li class="right" > <a href="counter.html" title="Counter" >previous</a> |</li> <li><a href="../contents.html">PyMOTW</a> »</li> <li><a href="../data_types.html" >Data Types</a> »</li> <li><a href="index.html" >collections – Container data types</a> »</li> </ul> </div> <div class="footer"> © Copyright Doug Hellmann. Last updated on Oct 24, 2010. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>. <br/><a href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/" rel="license"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/3.0/us/88x31.png"/></a> </div> </body> </html>