Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > media > contrib-backports > by-pkgid > 3ba3bd1608c672ba2129b098a48e9e4d > files > 635

python3-docs-3.2.2-3mdv2010.2.noarch.rpm



<!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>7.3. collections — Container datatypes &mdash; Python v3.2.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.2.2',
        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>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v3.2.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v3.2.2 documentation" href="../index.html" />
    <link rel="up" title="7. Data Types" href="datatypes.html" />
    <link rel="next" title="7.4. heapq — Heap queue algorithm" href="heapq.html" />
    <link rel="prev" title="7.2. calendar — General calendar-related functions" href="calendar.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </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="heapq.html" title="7.4. heapq — Heap queue algorithm"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="calendar.html" title="7.2. calendar — General calendar-related functions"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="datatypes.html" accesskey="U">7. Data Types</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-collections">
<span id="collections-container-datatypes"></span><h1>7.3. <a class="reference internal" href="#module-collections" title="collections: Container datatypes"><tt class="xref py py-mod docutils literal"><span class="pre">collections</span></tt></a> &#8212; Container datatypes<a class="headerlink" href="#module-collections" title="Permalink to this headline">¶</a></h1>
<p><strong>Source code:</strong> <a class="reference external" href="http://hg.python.org/cpython/file/3.2/Lib/collections.py">Lib/collections.py</a> and <a class="reference external" href="http://hg.python.org/cpython/file/3.2/Lib/_abcoll.py">Lib/_abcoll.py</a></p>
<hr class="docutils" />
<p>This module implements specialized container datatypes providing alternatives to
Python&#8217;s general purpose built-in containers, <a class="reference internal" href="stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a>, <a class="reference internal" href="functions.html#list" title="list"><tt class="xref py py-class docutils literal"><span class="pre">list</span></tt></a>,
<a class="reference internal" href="stdtypes.html#set" title="set"><tt class="xref py py-class docutils literal"><span class="pre">set</span></tt></a>, and <a class="reference internal" href="functions.html#tuple" title="tuple"><tt class="xref py py-class docutils literal"><span class="pre">tuple</span></tt></a>.</p>
<table border="1" class="docutils">
<colgroup>
<col width="24%" />
<col width="76%" />
</colgroup>
<tbody valign="top">
<tr><td><a class="reference internal" href="#collections.namedtuple" title="collections.namedtuple"><tt class="xref py py-func docutils literal"><span class="pre">namedtuple()</span></tt></a></td>
<td>factory function for creating tuple subclasses with named fields</td>
</tr>
<tr><td><a class="reference internal" href="#collections.deque" title="collections.deque"><tt class="xref py py-class docutils literal"><span class="pre">deque</span></tt></a></td>
<td>list-like container with fast appends and pops on either end</td>
</tr>
<tr><td><a class="reference internal" href="#collections.Counter" title="collections.Counter"><tt class="xref py py-class docutils literal"><span class="pre">Counter</span></tt></a></td>
<td>dict subclass for counting hashable objects</td>
</tr>
<tr><td><a class="reference internal" href="#collections.OrderedDict" title="collections.OrderedDict"><tt class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></tt></a></td>
<td>dict subclass that remembers the order entries were added</td>
</tr>
<tr><td><a class="reference internal" href="#collections.defaultdict" title="collections.defaultdict"><tt class="xref py py-class docutils literal"><span class="pre">defaultdict</span></tt></a></td>
<td>dict subclass that calls a factory function to supply missing values</td>
</tr>
<tr><td><a class="reference internal" href="#collections.UserDict" title="collections.UserDict"><tt class="xref py py-class docutils literal"><span class="pre">UserDict</span></tt></a></td>
<td>wrapper around dictionary objects for easier dict subclassing</td>
</tr>
<tr><td><a class="reference internal" href="#collections.UserList" title="collections.UserList"><tt class="xref py py-class docutils literal"><span class="pre">UserList</span></tt></a></td>
<td>wrapper around list objects for easier list subclassing</td>
</tr>
<tr><td><a class="reference internal" href="#collections.UserString" title="collections.UserString"><tt class="xref py py-class docutils literal"><span class="pre">UserString</span></tt></a></td>
<td>wrapper around string objects for easier string subclassing</td>
</tr>
</tbody>
</table>
<p>In addition to the concrete container classes, the collections module provides
<a class="reference internal" href="#collections-abstract-base-classes"><em>abstract base classes</em></a> that can be
used to test whether a class provides a particular interface, for example,
whether it is hashable or a mapping.</p>
<div class="section" id="counter-objects">
<h2>7.3.1. <a class="reference internal" href="#collections.Counter" title="collections.Counter"><tt class="xref py py-class docutils literal"><span class="pre">Counter</span></tt></a> objects<a class="headerlink" href="#counter-objects" title="Permalink to this headline">¶</a></h2>
<p>A counter tool is provided to support convenient and rapid tallies.
For example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># Tally occurrences of words in a list</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cnt</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;red&#39;</span><span class="p">,</span> <span class="s">&#39;blue&#39;</span><span class="p">,</span> <span class="s">&#39;red&#39;</span><span class="p">,</span> <span class="s">&#39;green&#39;</span><span class="p">,</span> <span class="s">&#39;blue&#39;</span><span class="p">,</span> <span class="s">&#39;blue&#39;</span><span class="p">]:</span>
<span class="gp">... </span>    <span class="n">cnt</span><span class="p">[</span><span class="n">word</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cnt</span>
<span class="go">Counter({&#39;blue&#39;: 3, &#39;red&#39;: 2, &#39;green&#39;: 1})</span>

<span class="gp">&gt;&gt;&gt; </span><span class="c"># Find the ten most common words in Hamlet</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">re</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">words</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s">&#39;\w+&#39;</span><span class="p">,</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;hamlet.txt&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Counter</span><span class="p">(</span><span class="n">words</span><span class="p">)</span><span class="o">.</span><span class="n">most_common</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="go">[(&#39;the&#39;, 1143), (&#39;and&#39;, 966), (&#39;to&#39;, 762), (&#39;of&#39;, 669), (&#39;i&#39;, 631),</span>
<span class="go"> (&#39;you&#39;, 554),  (&#39;a&#39;, 546), (&#39;my&#39;, 514), (&#39;hamlet&#39;, 471), (&#39;in&#39;, 451)]</span>
</pre></div>
</div>
<dl class="class">
<dt id="collections.Counter">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">Counter</tt><big>(</big><span class="optional">[</span><em>iterable-or-mapping</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#collections.Counter" title="Permalink to this definition">¶</a></dt>
<dd><p>A <a class="reference internal" href="#collections.Counter" title="collections.Counter"><tt class="xref py py-class docutils literal"><span class="pre">Counter</span></tt></a> is a <a class="reference internal" href="stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a> subclass for counting hashable objects.
It is an unordered collection where elements are stored as dictionary keys
and their counts are stored as dictionary values.  Counts are allowed to be
any integer value including zero or negative counts.  The <a class="reference internal" href="#collections.Counter" title="collections.Counter"><tt class="xref py py-class docutils literal"><span class="pre">Counter</span></tt></a>
class is similar to bags or multisets in other languages.</p>
<p>Elements are counted from an <em>iterable</em> or initialized from another
<em>mapping</em> (or counter):</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">()</span>                           <span class="c"># a new, empty counter</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="s">&#39;gallahad&#39;</span><span class="p">)</span>                 <span class="c"># a new counter from an iterable</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">({</span><span class="s">&#39;red&#39;</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="s">&#39;blue&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">})</span>      <span class="c"># a new counter from a mapping</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">cats</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">dogs</span><span class="o">=</span><span class="mi">8</span><span class="p">)</span>             <span class="c"># a new counter from keyword args</span>
</pre></div>
</div>
<p>Counter objects have a dictionary interface except that they return a zero
count for missing items instead of raising a <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">([</span><span class="s">&#39;eggs&#39;</span><span class="p">,</span> <span class="s">&#39;ham&#39;</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s">&#39;bacon&#39;</span><span class="p">]</span>                              <span class="c"># count of a missing element is zero</span>
<span class="go">0</span>
</pre></div>
</div>
<p>Setting a count to zero does not remove an element from a counter.
Use <tt class="docutils literal"><span class="pre">del</span></tt> to remove it entirely:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s">&#39;sausage&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>                        <span class="c"># counter entry with a zero count</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">c</span><span class="p">[</span><span class="s">&#39;sausage&#39;</span><span class="p">]</span>                        <span class="c"># del actually removes the entry</span>
</pre></div>
</div>
<p class="versionadded">
<span class="versionmodified">New in version 3.1.</span></p>
<p>Counter objects support three methods beyond those available for all
dictionaries:</p>
<dl class="method">
<dt id="collections.Counter.elements">
<tt class="descname">elements</tt><big>(</big><big>)</big><a class="headerlink" href="#collections.Counter.elements" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an iterator over elements repeating each as many times as its
count.  Elements are returned in arbitrary order.  If an element&#8217;s count
is less than one, <a class="reference internal" href="#collections.Counter.elements" title="collections.Counter.elements"><tt class="xref py py-meth docutils literal"><span class="pre">elements()</span></tt></a> will ignore it.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">d</span><span class="o">=-</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">elements</span><span class="p">())</span>
<span class="go">[&#39;a&#39;, &#39;a&#39;, &#39;a&#39;, &#39;a&#39;, &#39;b&#39;, &#39;b&#39;]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="collections.Counter.most_common">
<tt class="descname">most_common</tt><big>(</big><span class="optional">[</span><em>n</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#collections.Counter.most_common" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a list of the <em>n</em> most common elements and their counts from the
most common to the least.  If <em>n</em> is not specified, <a class="reference internal" href="#collections.Counter.most_common" title="collections.Counter.most_common"><tt class="xref py py-func docutils literal"><span class="pre">most_common()</span></tt></a>
returns <em>all</em> elements in the counter.  Elements with equal counts are
ordered arbitrarily:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Counter</span><span class="p">(</span><span class="s">&#39;abracadabra&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">most_common</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">[(&#39;a&#39;, 5), (&#39;r&#39;, 2), (&#39;b&#39;, 2)]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="collections.Counter.subtract">
<tt class="descname">subtract</tt><big>(</big><span class="optional">[</span><em>iterable-or-mapping</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#collections.Counter.subtract" title="Permalink to this definition">¶</a></dt>
<dd><p>Elements are subtracted from an <em>iterable</em> or from another <em>mapping</em>
(or counter).  Like <a class="reference internal" href="stdtypes.html#dict.update" title="dict.update"><tt class="xref py py-meth docutils literal"><span class="pre">dict.update()</span></tt></a> but subtracts counts instead
of replacing them.  Both inputs and outputs may be zero or negative.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">d</span><span class="o">=-</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">d</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="go">Counter({&#39;a&#39;: 3, &#39;b&#39;: 0, &#39;c&#39;: -3, &#39;d&#39;: -6})</span>
</pre></div>
</div>
<p class="versionadded">
<span class="versionmodified">New in version 3.2.</span></p>
</dd></dl>

<p>The usual dictionary methods are available for <a class="reference internal" href="#collections.Counter" title="collections.Counter"><tt class="xref py py-class docutils literal"><span class="pre">Counter</span></tt></a> objects
except for two which work differently for counters.</p>
<dl class="method">
<dt id="collections.Counter.fromkeys">
<tt class="descname">fromkeys</tt><big>(</big><em>iterable</em><big>)</big><a class="headerlink" href="#collections.Counter.fromkeys" title="Permalink to this definition">¶</a></dt>
<dd><p>This class method is not implemented for <a class="reference internal" href="#collections.Counter" title="collections.Counter"><tt class="xref py py-class docutils literal"><span class="pre">Counter</span></tt></a> objects.</p>
</dd></dl>

<dl class="method">
<dt id="collections.Counter.update">
<tt class="descname">update</tt><big>(</big><span class="optional">[</span><em>iterable-or-mapping</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#collections.Counter.update" title="Permalink to this definition">¶</a></dt>
<dd><p>Elements are counted from an <em>iterable</em> or added-in from another
<em>mapping</em> (or counter).  Like <a class="reference internal" href="stdtypes.html#dict.update" title="dict.update"><tt class="xref py py-meth docutils literal"><span class="pre">dict.update()</span></tt></a> but adds counts
instead of replacing them.  Also, the <em>iterable</em> is expected to be a
sequence of elements, not a sequence of <tt class="docutils literal"><span class="pre">(key,</span> <span class="pre">value)</span></tt> pairs.</p>
</dd></dl>

</dd></dl>

<p>Common patterns for working with <a class="reference internal" href="#collections.Counter" title="collections.Counter"><tt class="xref py py-class docutils literal"><span class="pre">Counter</span></tt></a> objects:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="nb">sum</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>                 <span class="c"># total of all counts</span>
<span class="n">c</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>                       <span class="c"># reset all counts</span>
<span class="nb">list</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>                         <span class="c"># list unique elements</span>
<span class="nb">set</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>                          <span class="c"># convert to a set</span>
<span class="nb">dict</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>                         <span class="c"># convert to a regular dictionary</span>
<span class="n">c</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>                       <span class="c"># convert to a list of (elem, cnt) pairs</span>
<span class="n">Counter</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">list_of_pairs</span><span class="p">))</span>    <span class="c"># convert from a list of (elem, cnt) pairs</span>
<span class="n">c</span><span class="o">.</span><span class="n">most_common</span><span class="p">()[:</span><span class="o">-</span><span class="n">n</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>         <span class="c"># n least common elements</span>
<span class="n">c</span> <span class="o">+=</span> <span class="n">Counter</span><span class="p">()</span>                  <span class="c"># remove zero and negative counts</span>
</pre></div>
</div>
<p>Several mathematical operations are provided for combining <a class="reference internal" href="#collections.Counter" title="collections.Counter"><tt class="xref py py-class docutils literal"><span class="pre">Counter</span></tt></a>
objects to produce multisets (counters that have counts greater than zero).
Addition and subtraction combine counters by adding or subtracting the counts
of corresponding elements.  Intersection and union return the minimum and
maximum of corresponding counts.  Each operation can accept inputs with signed
counts, but the output will exclude results with counts of zero or less.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">+</span> <span class="n">d</span>                       <span class="c"># add two counters together:  c[x] + d[x]</span>
<span class="go">Counter({&#39;a&#39;: 4, &#39;b&#39;: 3})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">-</span> <span class="n">d</span>                       <span class="c"># subtract (keeping only positive counts)</span>
<span class="go">Counter({&#39;a&#39;: 2})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">&amp;</span> <span class="n">d</span>                       <span class="c"># intersection:  min(c[x], d[x])</span>
<span class="go">Counter({&#39;a&#39;: 1, &#39;b&#39;: 1})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">|</span> <span class="n">d</span>                       <span class="c"># union:  max(c[x], d[x])</span>
<span class="go">Counter({&#39;a&#39;: 3, &#39;b&#39;: 2})</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Counters were primarily designed to work with positive integers to represent
running counts; however, care was taken to not unnecessarily preclude use
cases needing other types or negative values.  To help with those use cases,
this section documents the minimum range and type restrictions.</p>
<ul class="last simple">
<li>The <a class="reference internal" href="#collections.Counter" title="collections.Counter"><tt class="xref py py-class docutils literal"><span class="pre">Counter</span></tt></a> class itself is a dictionary subclass with no
restrictions on its keys and values.  The values are intended to be numbers
representing counts, but you <em>could</em> store anything in the value field.</li>
<li>The <tt class="xref py py-meth docutils literal"><span class="pre">most_common()</span></tt> method requires only that the values be orderable.</li>
<li>For in-place operations such as <tt class="docutils literal"><span class="pre">c[key]</span> <span class="pre">+=</span> <span class="pre">1</span></tt>, the value type need only
support addition and subtraction.  So fractions, floats, and decimals would
work and negative values are supported.  The same is also true for
<tt class="xref py py-meth docutils literal"><span class="pre">update()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">subtract()</span></tt> which allow negative and zero values
for both inputs and outputs.</li>
<li>The multiset methods are designed only for use cases with positive values.
The inputs may be negative or zero, but only outputs with positive values
are created.  There are no type restrictions, but the value type needs to
support support addition, subtraction, and comparison.</li>
<li>The <tt class="xref py py-meth docutils literal"><span class="pre">elements()</span></tt> method requires integer counts.  It ignores zero and
negative counts.</li>
</ul>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<ul class="last">
<li><p class="first"><a class="reference external" href="http://code.activestate.com/recipes/576611/">Counter class</a>
adapted for Python 2.5 and an early <a class="reference external" href="http://code.activestate.com/recipes/259174/">Bag recipe</a> for Python 2.4.</p>
</li>
<li><p class="first"><a class="reference external" href="http://www.gnu.org/software/smalltalk/manual-base/html_node/Bag.html">Bag class</a>
in Smalltalk.</p>
</li>
<li><p class="first">Wikipedia entry for <a class="reference external" href="http://en.wikipedia.org/wiki/Multiset">Multisets</a>.</p>
</li>
<li><p class="first"><a class="reference external" href="http://www.demo2s.com/Tutorial/Cpp/0380__set-multiset/Catalog0380__set-multiset.htm">C++ multisets</a>
tutorial with examples.</p>
</li>
<li><p class="first">For mathematical operations on multisets and their use cases, see
<em>Knuth, Donald. The Art of Computer Programming Volume II,
Section 4.6.3, Exercise 19</em>.</p>
</li>
<li><p class="first">To enumerate all distinct multisets of a given size over a given set of
elements, see <a class="reference internal" href="itertools.html#itertools.combinations_with_replacement" title="itertools.combinations_with_replacement"><tt class="xref py py-func docutils literal"><span class="pre">itertools.combinations_with_replacement()</span></tt></a>.</p>
<blockquote>
<div><p>map(Counter, combinations_with_replacement(&#8216;ABC&#8217;, 2)) &#8211;&gt; AA AB AC BB BC CC</p>
</div></blockquote>
</li>
</ul>
</div>
</div>
<div class="section" id="deque-objects">
<h2>7.3.2. <a class="reference internal" href="#collections.deque" title="collections.deque"><tt class="xref py py-class docutils literal"><span class="pre">deque</span></tt></a> objects<a class="headerlink" href="#deque-objects" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="collections.deque">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">deque</tt><big>(</big><span class="optional">[</span><em>iterable</em><span class="optional">[</span>, <em>maxlen</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#collections.deque" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a new deque object initialized left-to-right (using <a class="reference internal" href="#collections.deque.append" title="collections.deque.append"><tt class="xref py py-meth docutils literal"><span class="pre">append()</span></tt></a>) with
data from <em>iterable</em>.  If <em>iterable</em> is not specified, the new deque is empty.</p>
<p>Deques are a generalization of stacks and queues (the name is pronounced &#8220;deck&#8221;
and is short for &#8220;double-ended queue&#8221;).  Deques support thread-safe, memory
efficient appends and pops from either side of the deque with approximately the
same O(1) performance in either direction.</p>
<p>Though <a class="reference internal" href="functions.html#list" title="list"><tt class="xref py py-class docutils literal"><span class="pre">list</span></tt></a> objects support similar operations, they are optimized for
fast fixed-length operations and incur O(n) memory movement costs for
<tt class="docutils literal"><span class="pre">pop(0)</span></tt> and <tt class="docutils literal"><span class="pre">insert(0,</span> <span class="pre">v)</span></tt> operations which change both the size and
position of the underlying data representation.</p>
<p>If <em>maxlen</em> is not specified or is <em>None</em>, deques may grow to an
arbitrary length.  Otherwise, the deque is bounded to the specified maximum
length.  Once a bounded length deque is full, when new items are added, a
corresponding number of items are discarded from the opposite end.  Bounded
length deques provide functionality similar to the <tt class="docutils literal"><span class="pre">tail</span></tt> filter in
Unix. They are also useful for tracking transactions and other pools of data
where only the most recent activity is of interest.</p>
<p>Deque objects support the following methods:</p>
<dl class="method">
<dt id="collections.deque.append">
<tt class="descname">append</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#collections.deque.append" title="Permalink to this definition">¶</a></dt>
<dd><p>Add <em>x</em> to the right side of the deque.</p>
</dd></dl>

<dl class="method">
<dt id="collections.deque.appendleft">
<tt class="descname">appendleft</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#collections.deque.appendleft" title="Permalink to this definition">¶</a></dt>
<dd><p>Add <em>x</em> to the left side of the deque.</p>
</dd></dl>

<dl class="method">
<dt id="collections.deque.clear">
<tt class="descname">clear</tt><big>(</big><big>)</big><a class="headerlink" href="#collections.deque.clear" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove all elements from the deque leaving it with length 0.</p>
</dd></dl>

<dl class="method">
<dt id="collections.deque.count">
<tt class="descname">count</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#collections.deque.count" title="Permalink to this definition">¶</a></dt>
<dd><p>Count the number of deque elements equal to <em>x</em>.</p>
<p class="versionadded">
<span class="versionmodified">New in version 3.2.</span></p>
</dd></dl>

<dl class="method">
<dt id="collections.deque.extend">
<tt class="descname">extend</tt><big>(</big><em>iterable</em><big>)</big><a class="headerlink" href="#collections.deque.extend" title="Permalink to this definition">¶</a></dt>
<dd><p>Extend the right side of the deque by appending elements from the iterable
argument.</p>
</dd></dl>

<dl class="method">
<dt id="collections.deque.extendleft">
<tt class="descname">extendleft</tt><big>(</big><em>iterable</em><big>)</big><a class="headerlink" href="#collections.deque.extendleft" title="Permalink to this definition">¶</a></dt>
<dd><p>Extend the left side of the deque by appending elements from <em>iterable</em>.
Note, the series of left appends results in reversing the order of
elements in the iterable argument.</p>
</dd></dl>

<dl class="method">
<dt id="collections.deque.pop">
<tt class="descname">pop</tt><big>(</big><big>)</big><a class="headerlink" href="#collections.deque.pop" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove and return an element from the right side of the deque. If no
elements are present, raises an <a class="reference internal" href="exceptions.html#IndexError" title="IndexError"><tt class="xref py py-exc docutils literal"><span class="pre">IndexError</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="collections.deque.popleft">
<tt class="descname">popleft</tt><big>(</big><big>)</big><a class="headerlink" href="#collections.deque.popleft" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove and return an element from the left side of the deque. If no
elements are present, raises an <a class="reference internal" href="exceptions.html#IndexError" title="IndexError"><tt class="xref py py-exc docutils literal"><span class="pre">IndexError</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="collections.deque.remove">
<tt class="descname">remove</tt><big>(</big><em>value</em><big>)</big><a class="headerlink" href="#collections.deque.remove" title="Permalink to this definition">¶</a></dt>
<dd><p>Removed the first occurrence of <em>value</em>.  If not found, raises a
<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="collections.deque.reverse">
<tt class="descname">reverse</tt><big>(</big><big>)</big><a class="headerlink" href="#collections.deque.reverse" title="Permalink to this definition">¶</a></dt>
<dd><p>Reverse the elements of the deque in-place and then return <tt class="xref docutils literal"><span class="pre">None</span></tt>.</p>
<p class="versionadded">
<span class="versionmodified">New in version 3.2.</span></p>
</dd></dl>

<dl class="method">
<dt id="collections.deque.rotate">
<tt class="descname">rotate</tt><big>(</big><em>n</em><big>)</big><a class="headerlink" href="#collections.deque.rotate" title="Permalink to this definition">¶</a></dt>
<dd><p>Rotate the deque <em>n</em> steps to the right.  If <em>n</em> is negative, rotate to
the left.  Rotating one step to the right is equivalent to:
<tt class="docutils literal"><span class="pre">d.appendleft(d.pop())</span></tt>.</p>
</dd></dl>

<p>Deque objects also provide one read-only attribute:</p>
<dl class="attribute">
<dt id="collections.deque.maxlen">
<tt class="descname">maxlen</tt><a class="headerlink" href="#collections.deque.maxlen" title="Permalink to this definition">¶</a></dt>
<dd><p>Maximum size of a deque or <em>None</em> if unbounded.</p>
<p class="versionadded">
<span class="versionmodified">New in version 3.1.</span></p>
</dd></dl>

</dd></dl>

<p>In addition to the above, deques support iteration, pickling, <tt class="docutils literal"><span class="pre">len(d)</span></tt>,
<tt class="docutils literal"><span class="pre">reversed(d)</span></tt>, <tt class="docutils literal"><span class="pre">copy.copy(d)</span></tt>, <tt class="docutils literal"><span class="pre">copy.deepcopy(d)</span></tt>, membership testing with
the <a class="reference internal" href="../reference/expressions.html#in"><tt class="xref std std-keyword docutils literal"><span class="pre">in</span></tt></a> operator, and subscript references such as <tt class="docutils literal"><span class="pre">d[-1]</span></tt>.  Indexed
access is O(1) at both ends but slows to O(n) in the middle.  For fast random
access, use lists instead.</p>
<p>Example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">deque</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">deque</span><span class="p">(</span><span class="s">&#39;ghi&#39;</span><span class="p">)</span>                 <span class="c"># make a new deque with three items</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">d</span><span class="p">:</span>                   <span class="c"># iterate over the deque&#39;s elements</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">elem</span><span class="o">.</span><span class="n">upper</span><span class="p">())</span>
<span class="go">G</span>
<span class="go">H</span>
<span class="go">I</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;j&#39;</span><span class="p">)</span>                    <span class="c"># add a new entry to the right side</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">appendleft</span><span class="p">(</span><span class="s">&#39;f&#39;</span><span class="p">)</span>                <span class="c"># add a new entry to the left side</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span>                                <span class="c"># show the representation of the deque</span>
<span class="go">deque([&#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>                          <span class="c"># return and remove the rightmost item</span>
<span class="go">&#39;j&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span>                      <span class="c"># return and remove the leftmost item</span>
<span class="go">&#39;f&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>                          <span class="c"># list the contents of the deque</span>
<span class="go">[&#39;g&#39;, &#39;h&#39;, &#39;i&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>                             <span class="c"># peek at leftmost item</span>
<span class="go">&#39;g&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>                            <span class="c"># peek at rightmost item</span>
<span class="go">&#39;i&#39;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="n">d</span><span class="p">))</span>                <span class="c"># list the contents of a deque in reverse</span>
<span class="go">[&#39;i&#39;, &#39;h&#39;, &#39;g&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;h&#39;</span> <span class="ow">in</span> <span class="n">d</span>                         <span class="c"># search the deque</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="s">&#39;jkl&#39;</span><span class="p">)</span>                  <span class="c"># add multiple elements at once</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span>
<span class="go">deque([&#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;, &#39;k&#39;, &#39;l&#39;])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">rotate</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>                      <span class="c"># right rotation</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span>
<span class="go">deque([&#39;l&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;, &#39;k&#39;])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">rotate</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>                     <span class="c"># left rotation</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span>
<span class="go">deque([&#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;, &#39;k&#39;, &#39;l&#39;])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">deque</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="n">d</span><span class="p">))</span>               <span class="c"># make a new deque in reverse order</span>
<span class="go">deque([&#39;l&#39;, &#39;k&#39;, &#39;j&#39;, &#39;i&#39;, &#39;h&#39;, &#39;g&#39;])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>                        <span class="c"># empty the deque</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>                          <span class="c"># cannot pop from an empty deque</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;pyshell#6&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n-Identifier">-toplevel-</span>
    <span class="n">d</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
<span class="nc">IndexError</span>: <span class="n-Identifier">pop from an empty deque</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">extendleft</span><span class="p">(</span><span class="s">&#39;abc&#39;</span><span class="p">)</span>              <span class="c"># extendleft() reverses the input order</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span>
<span class="go">deque([&#39;c&#39;, &#39;b&#39;, &#39;a&#39;])</span>
</pre></div>
</div>
<div class="section" id="deque-recipes">
<h3>7.3.2.1. <a class="reference internal" href="#collections.deque" title="collections.deque"><tt class="xref py py-class docutils literal"><span class="pre">deque</span></tt></a> Recipes<a class="headerlink" href="#deque-recipes" title="Permalink to this headline">¶</a></h3>
<p>This section shows various approaches to working with deques.</p>
<p>Bounded length deques provide functionality similar to the <tt class="docutils literal"><span class="pre">tail</span></tt> filter
in Unix:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">def</span> <span class="nf">tail</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">n</span><span class="o">=</span><span class="mi">10</span><span class="p">):</span>
    <span class="s">&#39;Return the last n lines of a file&#39;</span>
    <span class="k">return</span> <span class="n">deque</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">),</span> <span class="n">n</span><span class="p">)</span>
</pre></div>
</div>
<p>Another approach to using deques is to maintain a sequence of recently
added elements by appending to the right and popping to the left:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">def</span> <span class="nf">moving_average</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">n</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
    <span class="c"># moving_average([40, 30, 50, 46, 39, 44]) --&gt; 40.0 42.0 45.0 43.0</span>
    <span class="c"># http://en.wikipedia.org/wiki/Moving_average</span>
    <span class="n">it</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span>
    <span class="n">d</span> <span class="o">=</span> <span class="n">deque</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">islice</span><span class="p">(</span><span class="n">it</span><span class="p">,</span> <span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">))</span>
    <span class="n">d</span><span class="o">.</span><span class="n">appendleft</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">it</span><span class="p">:</span>
        <span class="n">s</span> <span class="o">+=</span> <span class="n">elem</span> <span class="o">-</span> <span class="n">d</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span>
        <span class="n">d</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">elem</span><span class="p">)</span>
        <span class="k">yield</span> <span class="n">s</span> <span class="o">/</span> <span class="n">n</span>
</pre></div>
</div>
<p>The <tt class="xref py py-meth docutils literal"><span class="pre">rotate()</span></tt> method provides a way to implement <a class="reference internal" href="#collections.deque" title="collections.deque"><tt class="xref py py-class docutils literal"><span class="pre">deque</span></tt></a> slicing and
deletion.  For example, a pure Python implementation of <tt class="docutils literal"><span class="pre">del</span> <span class="pre">d[n]</span></tt> relies on
the <tt class="xref py py-meth docutils literal"><span class="pre">rotate()</span></tt> method to position elements to be popped:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">def</span> <span class="nf">delete_nth</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
    <span class="n">d</span><span class="o">.</span><span class="n">rotate</span><span class="p">(</span><span class="o">-</span><span class="n">n</span><span class="p">)</span>
    <span class="n">d</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span>
    <span class="n">d</span><span class="o">.</span><span class="n">rotate</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
</pre></div>
</div>
<p>To implement <a class="reference internal" href="#collections.deque" title="collections.deque"><tt class="xref py py-class docutils literal"><span class="pre">deque</span></tt></a> slicing, use a similar approach applying
<tt class="xref py py-meth docutils literal"><span class="pre">rotate()</span></tt> to bring a target element to the left side of the deque. Remove
old entries with <tt class="xref py py-meth docutils literal"><span class="pre">popleft()</span></tt>, add new entries with <tt class="xref py py-meth docutils literal"><span class="pre">extend()</span></tt>, and then
reverse the rotation.
With minor variations on that approach, it is easy to implement Forth style
stack manipulations such as <tt class="docutils literal"><span class="pre">dup</span></tt>, <tt class="docutils literal"><span class="pre">drop</span></tt>, <tt class="docutils literal"><span class="pre">swap</span></tt>, <tt class="docutils literal"><span class="pre">over</span></tt>, <tt class="docutils literal"><span class="pre">pick</span></tt>,
<tt class="docutils literal"><span class="pre">rot</span></tt>, and <tt class="docutils literal"><span class="pre">roll</span></tt>.</p>
</div>
</div>
<div class="section" id="defaultdict-objects">
<h2>7.3.3. <a class="reference internal" href="#collections.defaultdict" title="collections.defaultdict"><tt class="xref py py-class docutils literal"><span class="pre">defaultdict</span></tt></a> objects<a class="headerlink" href="#defaultdict-objects" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="collections.defaultdict">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">defaultdict</tt><big>(</big><span class="optional">[</span><em>default_factory</em><span class="optional">[</span>, <em>...</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#collections.defaultdict" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a new dictionary-like object.  <a class="reference internal" href="#collections.defaultdict" title="collections.defaultdict"><tt class="xref py py-class docutils literal"><span class="pre">defaultdict</span></tt></a> is a subclass of the
built-in <a class="reference internal" href="stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a> class.  It overrides one method and adds one writable
instance variable.  The remaining functionality is the same as for the
<a class="reference internal" href="stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a> class and is not documented here.</p>
<p>The first argument provides the initial value for the <a class="reference internal" href="#collections.defaultdict.default_factory" title="collections.defaultdict.default_factory"><tt class="xref py py-attr docutils literal"><span class="pre">default_factory</span></tt></a>
attribute; it defaults to <tt class="xref docutils literal"><span class="pre">None</span></tt>. All remaining arguments are treated the same
as if they were passed to the <a class="reference internal" href="stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a> constructor, including keyword
arguments.</p>
<p><a class="reference internal" href="#collections.defaultdict" title="collections.defaultdict"><tt class="xref py py-class docutils literal"><span class="pre">defaultdict</span></tt></a> objects support the following method in addition to the
standard <a class="reference internal" href="stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a> operations:</p>
<dl class="method">
<dt id="collections.defaultdict.__missing__">
<tt class="descname">__missing__</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#collections.defaultdict.__missing__" title="Permalink to this definition">¶</a></dt>
<dd><p>If the <a class="reference internal" href="#collections.defaultdict.default_factory" title="collections.defaultdict.default_factory"><tt class="xref py py-attr docutils literal"><span class="pre">default_factory</span></tt></a> attribute is <tt class="xref docutils literal"><span class="pre">None</span></tt>, this raises a
<a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a> exception with the <em>key</em> as argument.</p>
<p>If <a class="reference internal" href="#collections.defaultdict.default_factory" title="collections.defaultdict.default_factory"><tt class="xref py py-attr docutils literal"><span class="pre">default_factory</span></tt></a> is not <tt class="xref docutils literal"><span class="pre">None</span></tt>, it is called without arguments
to provide a default value for the given <em>key</em>, this value is inserted in
the dictionary for the <em>key</em>, and returned.</p>
<p>If calling <a class="reference internal" href="#collections.defaultdict.default_factory" title="collections.defaultdict.default_factory"><tt class="xref py py-attr docutils literal"><span class="pre">default_factory</span></tt></a> raises an exception this exception is
propagated unchanged.</p>
<p>This method is called by the <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></tt></a> method of the
<a class="reference internal" href="stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a> class when the requested key is not found; whatever it
returns or raises is then returned or raised by <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></tt></a>.</p>
</dd></dl>

<p><a class="reference internal" href="#collections.defaultdict" title="collections.defaultdict"><tt class="xref py py-class docutils literal"><span class="pre">defaultdict</span></tt></a> objects support the following instance variable:</p>
<dl class="attribute">
<dt id="collections.defaultdict.default_factory">
<tt class="descname">default_factory</tt><a class="headerlink" href="#collections.defaultdict.default_factory" title="Permalink to this definition">¶</a></dt>
<dd><p>This attribute is used by the <a class="reference internal" href="#collections.defaultdict.__missing__" title="collections.defaultdict.__missing__"><tt class="xref py py-meth docutils literal"><span class="pre">__missing__()</span></tt></a> method; it is
initialized from the first argument to the constructor, if present, or to
<tt class="xref docutils literal"><span class="pre">None</span></tt>, if absent.</p>
</dd></dl>

</dd></dl>

<div class="section" id="defaultdict-examples">
<h3>7.3.3.1. <a class="reference internal" href="#collections.defaultdict" title="collections.defaultdict"><tt class="xref py py-class docutils literal"><span class="pre">defaultdict</span></tt></a> Examples<a class="headerlink" href="#defaultdict-examples" title="Permalink to this headline">¶</a></h3>
<p>Using <a class="reference internal" href="functions.html#list" title="list"><tt class="xref py py-class docutils literal"><span class="pre">list</span></tt></a> as the <tt class="xref py py-attr docutils literal"><span class="pre">default_factory</span></tt>, it is easy to group a
sequence of key-value pairs into a dictionary of lists:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="p">[(</span><span class="s">&#39;yellow&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;blue&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;yellow&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;blue&#39;</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;red&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">s</span><span class="p">:</span>
<span class="gp">... </span>    <span class="n">d</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
<span class="go">[(&#39;blue&#39;, [2, 4]), (&#39;red&#39;, [1]), (&#39;yellow&#39;, [1, 3])]</span>
</pre></div>
</div>
<p>When each key is encountered for the first time, it is not already in the
mapping; so an entry is automatically created using the <tt class="xref py py-attr docutils literal"><span class="pre">default_factory</span></tt>
function which returns an empty <a class="reference internal" href="functions.html#list" title="list"><tt class="xref py py-class docutils literal"><span class="pre">list</span></tt></a>.  The <tt class="xref py py-meth docutils literal"><span class="pre">list.append()</span></tt>
operation then attaches the value to the new list.  When keys are encountered
again, the look-up proceeds normally (returning the list for that key) and the
<tt class="xref py py-meth docutils literal"><span class="pre">list.append()</span></tt> operation adds another value to the list. This technique is
simpler and faster than an equivalent technique using <a class="reference internal" href="stdtypes.html#dict.setdefault" title="dict.setdefault"><tt class="xref py py-meth docutils literal"><span class="pre">dict.setdefault()</span></tt></a>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="p">{}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">s</span><span class="p">:</span>
<span class="gp">... </span>    <span class="n">d</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="p">[])</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
<span class="go">[(&#39;blue&#39;, [2, 4]), (&#39;red&#39;, [1]), (&#39;yellow&#39;, [1, 3])]</span>
</pre></div>
</div>
<p>Setting the <tt class="xref py py-attr docutils literal"><span class="pre">default_factory</span></tt> to <a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a> makes the
<a class="reference internal" href="#collections.defaultdict" title="collections.defaultdict"><tt class="xref py py-class docutils literal"><span class="pre">defaultdict</span></tt></a> useful for counting (like a bag or multiset in other
languages):</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="s">&#39;mississippi&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">s</span><span class="p">:</span>
<span class="gp">... </span>    <span class="n">d</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
<span class="go">[(&#39;i&#39;, 4), (&#39;p&#39;, 2), (&#39;s&#39;, 4), (&#39;m&#39;, 1)]</span>
</pre></div>
</div>
<p>When a letter is first encountered, it is missing from the mapping, so the
<tt class="xref py py-attr docutils literal"><span class="pre">default_factory</span></tt> function calls <a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-func docutils literal"><span class="pre">int()</span></tt></a> to supply a default count of
zero.  The increment operation then builds up the count for each letter.</p>
<p>The function <a class="reference internal" href="functions.html#int" title="int"><tt class="xref py py-func docutils literal"><span class="pre">int()</span></tt></a> which always returns zero is just a special case of
constant functions.  A faster and more flexible way to create constant functions
is to use a lambda function which can supply any constant value (not just
zero):</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">constant_factory</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="k">lambda</span><span class="p">:</span> <span class="n">value</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="n">constant_factory</span><span class="p">(</span><span class="s">&#39;&lt;missing&gt;&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;John&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;ran&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;%(name)s %(action)s to %(object)s&#39;</span> <span class="o">%</span> <span class="n">d</span>
<span class="go">&#39;John ran to &lt;missing&gt;&#39;</span>
</pre></div>
</div>
<p>Setting the <tt class="xref py py-attr docutils literal"><span class="pre">default_factory</span></tt> to <a class="reference internal" href="stdtypes.html#set" title="set"><tt class="xref py py-class docutils literal"><span class="pre">set</span></tt></a> makes the
<a class="reference internal" href="#collections.defaultdict" title="collections.defaultdict"><tt class="xref py py-class docutils literal"><span class="pre">defaultdict</span></tt></a> useful for building a dictionary of sets:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="p">[(</span><span class="s">&#39;red&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;blue&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;red&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;blue&#39;</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;red&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;blue&#39;</span><span class="p">,</span> <span class="mi">4</span><span class="p">)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">set</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">s</span><span class="p">:</span>
<span class="gp">... </span>    <span class="n">d</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
<span class="go">[(&#39;blue&#39;, set([2, 4])), (&#39;red&#39;, set([1, 3]))]</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="namedtuple-factory-function-for-tuples-with-named-fields">
<h2>7.3.4. <a class="reference internal" href="#collections.namedtuple" title="collections.namedtuple"><tt class="xref py py-func docutils literal"><span class="pre">namedtuple()</span></tt></a> Factory Function for Tuples with Named Fields<a class="headerlink" href="#namedtuple-factory-function-for-tuples-with-named-fields" title="Permalink to this headline">¶</a></h2>
<p>Named tuples assign meaning to each position in a tuple and allow for more readable,
self-documenting code.  They can be used wherever regular tuples are used, and
they add the ability to access fields by name instead of position index.</p>
<dl class="function">
<dt id="collections.namedtuple">
<tt class="descclassname">collections.</tt><tt class="descname">namedtuple</tt><big>(</big><em>typename</em>, <em>field_names</em>, <em>verbose=False</em>, <em>rename=False</em><big>)</big><a class="headerlink" href="#collections.namedtuple" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a new tuple subclass named <em>typename</em>.  The new subclass is used to
create tuple-like objects that have fields accessible by attribute lookup as
well as being indexable and iterable.  Instances of the subclass also have a
helpful docstring (with typename and field_names) and a helpful <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><tt class="xref py py-meth docutils literal"><span class="pre">__repr__()</span></tt></a>
method which lists the tuple contents in a <tt class="docutils literal"><span class="pre">name=value</span></tt> format.</p>
<p>The <em>field_names</em> are a single string with each fieldname separated by whitespace
and/or commas, for example <tt class="docutils literal"><span class="pre">'x</span> <span class="pre">y'</span></tt> or <tt class="docutils literal"><span class="pre">'x,</span> <span class="pre">y'</span></tt>.  Alternatively, <em>field_names</em>
can be a sequence of strings such as <tt class="docutils literal"><span class="pre">['x',</span> <span class="pre">'y']</span></tt>.</p>
<p>Any valid Python identifier may be used for a fieldname except for names
starting with an underscore.  Valid identifiers consist of letters, digits,
and underscores but do not start with a digit or underscore and cannot be
a <a class="reference internal" href="keyword.html#module-keyword" title="keyword: Test whether a string is a keyword in Python."><tt class="xref py py-mod docutils literal"><span class="pre">keyword</span></tt></a> such as <em>class</em>, <em>for</em>, <em>return</em>, <em>global</em>, <em>pass</em>,
or <em>raise</em>.</p>
<p>If <em>rename</em> is true, invalid fieldnames are automatically replaced
with positional names.  For example, <tt class="docutils literal"><span class="pre">['abc',</span> <span class="pre">'def',</span> <span class="pre">'ghi',</span> <span class="pre">'abc']</span></tt> is
converted to <tt class="docutils literal"><span class="pre">['abc',</span> <span class="pre">'_1',</span> <span class="pre">'ghi',</span> <span class="pre">'_3']</span></tt>, eliminating the keyword
<tt class="docutils literal"><span class="pre">def</span></tt> and the duplicate fieldname <tt class="docutils literal"><span class="pre">abc</span></tt>.</p>
<p>If <em>verbose</em> is true, the class definition is printed just before being built.</p>
<p>Named tuple instances do not have per-instance dictionaries, so they are
lightweight and require no more memory than regular tuples.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 3.1: </span>Added support for <em>rename</em>.</p>
</dd></dl>

<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># Basic example</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Point</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s">&#39;Point&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s">&#39;x&#39;</span><span class="p">,</span> <span class="s">&#39;y&#39;</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">11</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="c"># Example using the verbose option to print the class definition</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Point</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s">&#39;Point&#39;</span><span class="p">,</span> <span class="s">&#39;x y&#39;</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="k">True</span><span class="p">)</span>
<span class="go">class Point(tuple):</span>
<span class="go">        &#39;Point(x, y)&#39;</span>

<span class="go">        __slots__ = ()</span>

<span class="go">        _fields = (&#39;x&#39;, &#39;y&#39;)</span>

<span class="go">        def __new__(_cls, x, y):</span>
<span class="go">            &#39;Create a new instance of Point(x, y)&#39;</span>
<span class="go">            return _tuple.__new__(_cls, (x, y))</span>

<span class="go">        @classmethod</span>
<span class="go">        def _make(cls, iterable, new=tuple.__new__, len=len):</span>
<span class="go">            &#39;Make a new Point object from a sequence or iterable&#39;</span>
<span class="go">            result = new(cls, iterable)</span>
<span class="go">            if len(result) != 2:</span>
<span class="go">                raise TypeError(&#39;Expected 2 arguments, got %d&#39; % len(result))</span>
<span class="go">            return result</span>

<span class="go">        def __repr__(self):</span>
<span class="go">            &#39;Return a nicely formatted representation string&#39;</span>
<span class="go">            return self.__class__.__name__ + &#39;(x=%r, y=%r)&#39; % self</span>

<span class="go">        def _asdict(self):</span>
<span class="go">            &#39;Return a new OrderedDict which maps field names to their values&#39;</span>
<span class="go">            return OrderedDict(zip(self._fields, self))</span>

<span class="go">        __dict__ = property(_asdict)</span>

<span class="go">        def _replace(_self, **kwds):</span>
<span class="go">            &#39;Return a new Point object replacing specified fields with new values&#39;</span>
<span class="go">            result = _self._make(map(kwds.pop, (&#39;x&#39;, &#39;y&#39;), _self))</span>
<span class="go">            if kwds:</span>
<span class="go">                raise ValueError(&#39;Got unexpected field names: %r&#39; % list(kwds.keys()))</span>
<span class="go">            return result</span>

<span class="go">        def __getnewargs__(self):</span>
<span class="go">            &#39;Return self as a plain tuple.   Used by copy and pickle.&#39;</span>
<span class="go">            return tuple(self)</span>

<span class="go">        x = _property(_itemgetter(0), doc=&#39;Alias for field number 0&#39;)</span>
<span class="go">        y = _property(_itemgetter(1), doc=&#39;Alias for field number 1&#39;)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Point</span><span class="p">(</span><span class="mi">11</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">22</span><span class="p">)</span>     <span class="c"># instantiate with positional or keyword arguments</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>             <span class="c"># indexable like the plain tuple (11, 22)</span>
<span class="go">33</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">p</span>                <span class="c"># unpack like a regular tuple</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span>
<span class="go">(11, 22)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">x</span> <span class="o">+</span> <span class="n">p</span><span class="o">.</span><span class="n">y</span>               <span class="c"># fields also accessible by name</span>
<span class="go">33</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span>                       <span class="c"># readable __repr__ with a name=value style</span>
<span class="go">Point(x=11, y=22)</span>
</pre></div>
</div>
<p>Named tuples are especially useful for assigning field names to result tuples returned
by the <a class="reference internal" href="csv.html#module-csv" title="csv: Write and read tabular data to and from delimited files."><tt class="xref py py-mod docutils literal"><span class="pre">csv</span></tt></a> or <a class="reference internal" href="sqlite3.html#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> modules:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">EmployeeRecord</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s">&#39;EmployeeRecord&#39;</span><span class="p">,</span> <span class="s">&#39;name, age, title, department, paygrade&#39;</span><span class="p">)</span>

<span class="kn">import</span> <span class="nn">csv</span>
<span class="k">for</span> <span class="n">emp</span> <span class="ow">in</span> <span class="nb">map</span><span class="p">(</span><span class="n">EmployeeRecord</span><span class="o">.</span><span class="n">_make</span><span class="p">,</span> <span class="n">csv</span><span class="o">.</span><span class="n">reader</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="s">&quot;employees.csv&quot;</span><span class="p">,</span> <span class="s">&quot;rb&quot;</span><span class="p">))):</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">emp</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">emp</span><span class="o">.</span><span class="n">title</span><span class="p">)</span>

<span class="kn">import</span> <span class="nn">sqlite3</span>
<span class="n">conn</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&#39;/companydata&#39;</span><span class="p">)</span>
<span class="n">cursor</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&#39;SELECT name, age, title, department, paygrade FROM employees&#39;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">emp</span> <span class="ow">in</span> <span class="nb">map</span><span class="p">(</span><span class="n">EmployeeRecord</span><span class="o">.</span><span class="n">_make</span><span class="p">,</span> <span class="n">cursor</span><span class="o">.</span><span class="n">fetchall</span><span class="p">()):</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">emp</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">emp</span><span class="o">.</span><span class="n">title</span><span class="p">)</span>
</pre></div>
</div>
<p>In addition to the methods inherited from tuples, named tuples support
three additional methods and one attribute.  To prevent conflicts with
field names, the method and attribute names start with an underscore.</p>
<dl class="classmethod">
<dt id="collections.somenamedtuple._make">
<em class="property">classmethod </em><tt class="descclassname">somenamedtuple.</tt><tt class="descname">_make</tt><big>(</big><em>iterable</em><big>)</big><a class="headerlink" href="#collections.somenamedtuple._make" title="Permalink to this definition">¶</a></dt>
<dd><p>Class method that makes a new instance from an existing sequence or iterable.</p>
</dd></dl>

<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="p">[</span><span class="mi">11</span><span class="p">,</span> <span class="mi">22</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Point</span><span class="o">.</span><span class="n">_make</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
<span class="go">Point(x=11, y=22)</span>
</pre></div>
</div>
<dl class="method">
<dt id="collections.somenamedtuple._asdict">
<tt class="descclassname">somenamedtuple.</tt><tt class="descname">_asdict</tt><big>(</big><big>)</big><a class="headerlink" href="#collections.somenamedtuple._asdict" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a new <a class="reference internal" href="#collections.OrderedDict" title="collections.OrderedDict"><tt class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></tt></a> which maps field names to their corresponding
values:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">_asdict</span><span class="p">()</span>
<span class="go">OrderedDict([(&#39;x&#39;, 11), (&#39;y&#39;, 22)])</span>
</pre></div>
</div>
<p class="versionchanged">
<span class="versionmodified">Changed in version 3.1: </span>Returns an <a class="reference internal" href="#collections.OrderedDict" title="collections.OrderedDict"><tt class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></tt></a> instead of a regular <a class="reference internal" href="stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="collections.somenamedtuple._replace">
<tt class="descclassname">somenamedtuple.</tt><tt class="descname">_replace</tt><big>(</big><em>kwargs</em><big>)</big><a class="headerlink" href="#collections.somenamedtuple._replace" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a new instance of the named tuple replacing specified fields with new
values:</p>
</dd></dl>

<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">11</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">22</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">_replace</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">33</span><span class="p">)</span>
<span class="go">Point(x=33, y=22)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">partnum</span><span class="p">,</span> <span class="n">record</span> <span class="ow">in</span> <span class="n">inventory</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="gp">... </span>    <span class="n">inventory</span><span class="p">[</span><span class="n">partnum</span><span class="p">]</span> <span class="o">=</span> <span class="n">record</span><span class="o">.</span><span class="n">_replace</span><span class="p">(</span><span class="n">price</span><span class="o">=</span><span class="n">newprices</span><span class="p">[</span><span class="n">partnum</span><span class="p">],</span> <span class="n">timestamp</span><span class="o">=</span><span class="n">time</span><span class="o">.</span><span class="n">now</span><span class="p">())</span>
</pre></div>
</div>
<dl class="attribute">
<dt id="collections.somenamedtuple._fields">
<tt class="descclassname">somenamedtuple.</tt><tt class="descname">_fields</tt><a class="headerlink" href="#collections.somenamedtuple._fields" title="Permalink to this definition">¶</a></dt>
<dd><p>Tuple of strings listing the field names.  Useful for introspection
and for creating new named tuple types from existing named tuples.</p>
</dd></dl>

<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">_fields</span>            <span class="c"># view the field names</span>
<span class="go">(&#39;x&#39;, &#39;y&#39;)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">Color</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s">&#39;Color&#39;</span><span class="p">,</span> <span class="s">&#39;red green blue&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Pixel</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s">&#39;Pixel&#39;</span><span class="p">,</span> <span class="n">Point</span><span class="o">.</span><span class="n">_fields</span> <span class="o">+</span> <span class="n">Color</span><span class="o">.</span><span class="n">_fields</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Pixel</span><span class="p">(</span><span class="mi">11</span><span class="p">,</span> <span class="mi">22</span><span class="p">,</span> <span class="mi">128</span><span class="p">,</span> <span class="mi">255</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="go">Pixel(x=11, y=22, red=128, green=255, blue=0)</span>
</pre></div>
</div>
<p>To retrieve a field whose name is stored in a string, use the <a class="reference internal" href="functions.html#getattr" title="getattr"><tt class="xref py py-func docutils literal"><span class="pre">getattr()</span></tt></a>
function:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">getattr</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="s">&#39;x&#39;</span><span class="p">)</span>
<span class="go">11</span>
</pre></div>
</div>
<p>To convert a dictionary to a named tuple, use the double-star-operator
(as described in <a class="reference internal" href="../tutorial/controlflow.html#tut-unpacking-arguments"><em>Unpacking Argument Lists</em></a>):</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;x&#39;</span><span class="p">:</span> <span class="mi">11</span><span class="p">,</span> <span class="s">&#39;y&#39;</span><span class="p">:</span> <span class="mi">22</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Point</span><span class="p">(</span><span class="o">**</span><span class="n">d</span><span class="p">)</span>
<span class="go">Point(x=11, y=22)</span>
</pre></div>
</div>
<p>Since a named tuple is a regular Python class, it is easy to add or change
functionality with a subclass.  Here is how to add a calculated field and
a fixed-width print format:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Point</span><span class="p">(</span><span class="n">namedtuple</span><span class="p">(</span><span class="s">&#39;Point&#39;</span><span class="p">,</span> <span class="s">&#39;x y&#39;</span><span class="p">)):</span>
<span class="go">        __slots__ = ()</span>
<span class="go">        @property</span>
<span class="go">        def hypot(self):</span>
<span class="go">            return (self.x ** 2 + self.y ** 2) ** 0.5</span>
<span class="go">        def __str__(self):</span>
<span class="go">            return &#39;Point: x=%6.3f  y=%6.3f  hypot=%6.3f&#39; % (self.x, self.y, self.hypot)</span>
</pre></div>
</div>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">Point</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="n">Point</span><span class="p">(</span><span class="mi">14</span><span class="p">,</span> <span class="mi">5</span><span class="o">/</span><span class="mi">7</span><span class="p">):</span>
<span class="go">        print(p)</span>
<span class="go">Point: x= 3.000  y= 4.000  hypot= 5.000</span>
<span class="go">Point: x=14.000  y= 0.714  hypot=14.018</span>
</pre></div>
</div>
<p>The subclass shown above sets <tt class="docutils literal"><span class="pre">__slots__</span></tt> to an empty tuple.  This helps
keep memory requirements low by preventing the creation of instance dictionaries.</p>
<p>Subclassing is not useful for adding new, stored fields.  Instead, simply
create a new named tuple type from the <tt class="xref py py-attr docutils literal"><span class="pre">_fields</span></tt> attribute:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Point3D</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s">&#39;Point3D&#39;</span><span class="p">,</span> <span class="n">Point</span><span class="o">.</span><span class="n">_fields</span> <span class="o">+</span> <span class="p">(</span><span class="s">&#39;z&#39;</span><span class="p">,))</span>
</pre></div>
</div>
<p>Default values can be implemented by using <tt class="xref py py-meth docutils literal"><span class="pre">_replace()</span></tt> to
customize a prototype instance:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Account</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s">&#39;Account&#39;</span><span class="p">,</span> <span class="s">&#39;owner balance transaction_count&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">default_account</span> <span class="o">=</span> <span class="n">Account</span><span class="p">(</span><span class="s">&#39;&lt;owner name&gt;&#39;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">johns_account</span> <span class="o">=</span> <span class="n">default_account</span><span class="o">.</span><span class="n">_replace</span><span class="p">(</span><span class="n">owner</span><span class="o">=</span><span class="s">&#39;John&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Enumerated constants can be implemented with named tuples, but it is simpler
and more efficient to use a simple class declaration:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Status</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s">&#39;Status&#39;</span><span class="p">,</span> <span class="s">&#39;open pending closed&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">_make</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Status</span><span class="o">.</span><span class="n">open</span><span class="p">,</span> <span class="n">Status</span><span class="o">.</span><span class="n">pending</span><span class="p">,</span> <span class="n">Status</span><span class="o">.</span><span class="n">closed</span>
<span class="go">(0, 1, 2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Status</span><span class="p">:</span>
<span class="go">        open, pending, closed = range(3)</span>
</pre></div>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<ul class="last simple">
<li><a class="reference external" href="http://code.activestate.com/recipes/500261/">Named tuple recipe</a>
adapted for Python 2.4.</li>
<li><a class="reference external" href="http://code.activestate.com/recipes/577629-namedtupleabc-abstract-base-class-mix-in-for-named/">Recipe for named tuple abstract base class with a metaclass mix-in</a>
by Jan Kaliszewski.  Besides providing an <a class="reference internal" href="../glossary.html#term-abstract-base-class"><em class="xref std std-term">abstract base class</em></a> for
named tuples, it also supports an alternate <a class="reference internal" href="../glossary.html#term-metaclass"><em class="xref std std-term">metaclass</em></a>-based
constructor that is convenient for use cases where named tuples are being
subclassed.</li>
</ul>
</div>
</div>
<div class="section" id="ordereddict-objects">
<h2>7.3.5. <a class="reference internal" href="#collections.OrderedDict" title="collections.OrderedDict"><tt class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></tt></a> objects<a class="headerlink" href="#ordereddict-objects" title="Permalink to this headline">¶</a></h2>
<p>Ordered dictionaries are just like regular dictionaries but they remember the
order that items were inserted.  When iterating over an ordered dictionary,
the items are returned in the order their keys were first added.</p>
<dl class="class">
<dt id="collections.OrderedDict">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">OrderedDict</tt><big>(</big><span class="optional">[</span><em>items</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#collections.OrderedDict" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an instance of a dict subclass, supporting the usual <a class="reference internal" href="stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a>
methods.  An <em>OrderedDict</em> is a dict that remembers the order that keys
were first inserted. If a new entry overwrites an existing entry, the
original insertion position is left unchanged.  Deleting an entry and
reinserting it will move it to the end.</p>
<p class="versionadded">
<span class="versionmodified">New in version 3.1.</span></p>
<dl class="method">
<dt id="collections.OrderedDict.popitem">
<tt class="descname">popitem</tt><big>(</big><em>last=True</em><big>)</big><a class="headerlink" href="#collections.OrderedDict.popitem" title="Permalink to this definition">¶</a></dt>
<dd><p>The <a class="reference internal" href="#collections.OrderedDict.popitem" title="collections.OrderedDict.popitem"><tt class="xref py py-meth docutils literal"><span class="pre">popitem()</span></tt></a> method for ordered dictionaries returns and removes a
(key, value) pair.  The pairs are returned in LIFO order if <em>last</em> is true
or FIFO order if false.</p>
</dd></dl>

<dl class="method">
<dt id="collections.OrderedDict.move_to_end">
<tt class="descname">move_to_end</tt><big>(</big><em>key</em>, <em>last=True</em><big>)</big><a class="headerlink" href="#collections.OrderedDict.move_to_end" title="Permalink to this definition">¶</a></dt>
<dd><p>Move an existing <em>key</em> to either end of an ordered dictionary.  The item
is moved to the right end if <em>last</em> is true (the default) or to the
beginning if <em>last</em> is false.  Raises <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a> if the <em>key</em> does
not exist:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="o">.</span><span class="n">fromkeys</span><span class="p">(</span><span class="s">&#39;abcde&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">move_to_end</span><span class="p">(</span><span class="s">&#39;b&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
<span class="go">&#39;acdeb&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">move_to_end</span><span class="p">(</span><span class="s">&#39;b&#39;</span><span class="p">,</span> <span class="n">last</span><span class="o">=</span><span class="k">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
<span class="go">&#39;bacde&#39;</span>
</pre></div>
</div>
<p class="versionadded">
<span class="versionmodified">New in version 3.2.</span></p>
</dd></dl>

</dd></dl>

<p>In addition to the usual mapping methods, ordered dictionaries also support
reverse iteration using <a class="reference internal" href="functions.html#reversed" title="reversed"><tt class="xref py py-func docutils literal"><span class="pre">reversed()</span></tt></a>.</p>
<p>Equality tests between <a class="reference internal" href="#collections.OrderedDict" title="collections.OrderedDict"><tt class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></tt></a> objects are order-sensitive
and are implemented as <tt class="docutils literal"><span class="pre">list(od1.items())==list(od2.items())</span></tt>.
Equality tests between <a class="reference internal" href="#collections.OrderedDict" title="collections.OrderedDict"><tt class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></tt></a> objects and other
<a class="reference internal" href="#collections.Mapping" title="collections.Mapping"><tt class="xref py py-class docutils literal"><span class="pre">Mapping</span></tt></a> objects are order-insensitive like regular dictionaries.
This allows <a class="reference internal" href="#collections.OrderedDict" title="collections.OrderedDict"><tt class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></tt></a> objects to be substituted anywhere a
regular dictionary is used.</p>
<p>The <a class="reference internal" href="#collections.OrderedDict" title="collections.OrderedDict"><tt class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></tt></a> constructor and <tt class="xref py py-meth docutils literal"><span class="pre">update()</span></tt> method both accept
keyword arguments, but their order is lost because Python&#8217;s function call
semantics pass-in keyword arguments using a regular unordered dictionary.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference external" href="http://code.activestate.com/recipes/576693/">Equivalent OrderedDict recipe</a>
that runs on Python 2.4 or later.</p>
</div>
<div class="section" id="ordereddict-examples-and-recipes">
<h3>7.3.5.1. <a class="reference internal" href="#collections.OrderedDict" title="collections.OrderedDict"><tt class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></tt></a> Examples and Recipes<a class="headerlink" href="#ordereddict-examples-and-recipes" title="Permalink to this headline">¶</a></h3>
<p>Since an ordered dictionary remembers its insertion order, it can be used
in conjuction with sorting to make a sorted dictionary:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># regular unsorted dictionary</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;banana&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="s">&#39;apple&#39;</span><span class="p">:</span><span class="mi">4</span><span class="p">,</span> <span class="s">&#39;pear&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s">&#39;orange&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">}</span>

<span class="gp">&gt;&gt;&gt; </span><span class="c"># dictionary sorted by key</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">OrderedDict</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">(),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span>
<span class="go">OrderedDict([(&#39;apple&#39;, 4), (&#39;banana&#39;, 3), (&#39;orange&#39;, 2), (&#39;pear&#39;, 1)])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="c"># dictionary sorted by value</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">OrderedDict</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">(),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="n">t</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
<span class="go">OrderedDict([(&#39;pear&#39;, 1), (&#39;orange&#39;, 2), (&#39;banana&#39;, 3), (&#39;apple&#39;, 4)])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="c"># dictionary sorted by length of the key string</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">OrderedDict</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">(),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="nb">len</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">])))</span>
<span class="go">OrderedDict([(&#39;pear&#39;, 1), (&#39;apple&#39;, 4), (&#39;orange&#39;, 2), (&#39;banana&#39;, 3)])</span>
</pre></div>
</div>
<p>The new sorted dictionaries maintain their sort order when entries
are deleted.  But when new keys are added, the keys are appended
to the end and the sort is not maintained.</p>
<p>It is also straight-forward to create an ordered dictionary variant
that the remembers the order the keys were <em>last</em> inserted.
If a new entry overwrites an existing entry, the
original insertion position is changed and moved to the end:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">class</span> <span class="nc">LastUpdatedOrderedDict</span><span class="p">(</span><span class="n">OrderedDict</span><span class="p">):</span>
    <span class="s">&#39;Store items in the order the keys were last added&#39;</span>

    <span class="k">def</span> <span class="nf">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="p">:</span>
            <span class="k">del</span> <span class="bp">self</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
        <span class="n">OrderedDict</span><span class="o">.</span><span class="n">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</pre></div>
</div>
<p>An ordered dictionary can be combined with the <a class="reference internal" href="#collections.Counter" title="collections.Counter"><tt class="xref py py-class docutils literal"><span class="pre">Counter</span></tt></a> class
so that the counter remembers the order elements are first encountered:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">class</span> <span class="nc">OrderedCounter</span><span class="p">(</span><span class="n">Counter</span><span class="p">,</span> <span class="n">OrderedDict</span><span class="p">):</span>
     <span class="s">&#39;Counter that remembers the order elements are first encountered&#39;</span>

     <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
         <span class="k">return</span> <span class="s">&#39;%s(%r)&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="n">OrderedDict</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>

     <span class="k">def</span> <span class="nf">__reduce__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
         <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="p">,</span> <span class="p">(</span><span class="n">OrderedDict</span><span class="p">(</span><span class="bp">self</span><span class="p">),)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="userdict-objects">
<h2>7.3.6. <a class="reference internal" href="#collections.UserDict" title="collections.UserDict"><tt class="xref py py-class docutils literal"><span class="pre">UserDict</span></tt></a> objects<a class="headerlink" href="#userdict-objects" title="Permalink to this headline">¶</a></h2>
<p>The class, <a class="reference internal" href="#collections.UserDict" title="collections.UserDict"><tt class="xref py py-class docutils literal"><span class="pre">UserDict</span></tt></a> acts as a wrapper around dictionary objects.
The need for this class has been partially supplanted by the ability to
subclass directly from <a class="reference internal" href="stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a>; however, this class can be easier
to work with because the underlying dictionary is accessible as an
attribute.</p>
<dl class="class">
<dt id="collections.UserDict">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">UserDict</tt><big>(</big><span class="optional">[</span><em>initialdata</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#collections.UserDict" title="Permalink to this definition">¶</a></dt>
<dd><p>Class that simulates a dictionary.  The instance&#8217;s contents are kept in a
regular dictionary, which is accessible via the <a class="reference internal" href="#collections.UserDict.data" title="collections.UserDict.data"><tt class="xref py py-attr docutils literal"><span class="pre">data</span></tt></a> attribute of
<a class="reference internal" href="#collections.UserDict" title="collections.UserDict"><tt class="xref py py-class docutils literal"><span class="pre">UserDict</span></tt></a> instances.  If <em>initialdata</em> is provided, <a class="reference internal" href="#collections.UserDict.data" title="collections.UserDict.data"><tt class="xref py py-attr docutils literal"><span class="pre">data</span></tt></a> is
initialized with its contents; note that a reference to <em>initialdata</em> will not
be kept, allowing it be used for other purposes.</p>
<p>In addition to supporting the methods and operations of mappings,
<a class="reference internal" href="#collections.UserDict" title="collections.UserDict"><tt class="xref py py-class docutils literal"><span class="pre">UserDict</span></tt></a> instances provide the following attribute:</p>
<dl class="attribute">
<dt id="collections.UserDict.data">
<tt class="descname">data</tt><a class="headerlink" href="#collections.UserDict.data" title="Permalink to this definition">¶</a></dt>
<dd><p>A real dictionary used to store the contents of the <a class="reference internal" href="#collections.UserDict" title="collections.UserDict"><tt class="xref py py-class docutils literal"><span class="pre">UserDict</span></tt></a>
class.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="userlist-objects">
<h2>7.3.7. <a class="reference internal" href="#collections.UserList" title="collections.UserList"><tt class="xref py py-class docutils literal"><span class="pre">UserList</span></tt></a> objects<a class="headerlink" href="#userlist-objects" title="Permalink to this headline">¶</a></h2>
<p>This class acts as a wrapper around list objects.  It is a useful base class
for your own list-like classes which can inherit from them and override
existing methods or add new ones.  In this way, one can add new behaviors to
lists.</p>
<p>The need for this class has been partially supplanted by the ability to
subclass directly from <a class="reference internal" href="functions.html#list" title="list"><tt class="xref py py-class docutils literal"><span class="pre">list</span></tt></a>; however, this class can be easier
to work with because the underlying list is accessible as an attribute.</p>
<dl class="class">
<dt id="collections.UserList">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">UserList</tt><big>(</big><span class="optional">[</span><em>list</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#collections.UserList" title="Permalink to this definition">¶</a></dt>
<dd><p>Class that simulates a list.  The instance&#8217;s contents are kept in a regular
list, which is accessible via the <a class="reference internal" href="#collections.UserList.data" title="collections.UserList.data"><tt class="xref py py-attr docutils literal"><span class="pre">data</span></tt></a> attribute of <a class="reference internal" href="#collections.UserList" title="collections.UserList"><tt class="xref py py-class docutils literal"><span class="pre">UserList</span></tt></a>
instances.  The instance&#8217;s contents are initially set to a copy of <em>list</em>,
defaulting to the empty list <tt class="docutils literal"><span class="pre">[]</span></tt>.  <em>list</em> can be any iterable, for
example a real Python list or a <a class="reference internal" href="#collections.UserList" title="collections.UserList"><tt class="xref py py-class docutils literal"><span class="pre">UserList</span></tt></a> object.</p>
<p>In addition to supporting the methods and operations of mutable sequences,
<a class="reference internal" href="#collections.UserList" title="collections.UserList"><tt class="xref py py-class docutils literal"><span class="pre">UserList</span></tt></a> instances provide the following attribute:</p>
<dl class="attribute">
<dt id="collections.UserList.data">
<tt class="descname">data</tt><a class="headerlink" href="#collections.UserList.data" title="Permalink to this definition">¶</a></dt>
<dd><p>A real <a class="reference internal" href="functions.html#list" title="list"><tt class="xref py py-class docutils literal"><span class="pre">list</span></tt></a> object used to store the contents of the
<a class="reference internal" href="#collections.UserList" title="collections.UserList"><tt class="xref py py-class docutils literal"><span class="pre">UserList</span></tt></a> class.</p>
</dd></dl>

</dd></dl>

<p><strong>Subclassing requirements:</strong> Subclasses of <a class="reference internal" href="#collections.UserList" title="collections.UserList"><tt class="xref py py-class docutils literal"><span class="pre">UserList</span></tt></a> are expect to
offer a constructor which can be called with either no arguments or one
argument.  List operations which return a new sequence attempt to create an
instance of the actual implementation class.  To do so, it assumes that the
constructor can be called with a single parameter, which is a sequence object
used as a data source.</p>
<p>If a derived class does not wish to comply with this requirement, all of the
special methods supported by this class will need to be overridden; please
consult the sources for information about the methods which need to be provided
in that case.</p>
</div>
<div class="section" id="userstring-objects">
<h2>7.3.8. <a class="reference internal" href="#collections.UserString" title="collections.UserString"><tt class="xref py py-class docutils literal"><span class="pre">UserString</span></tt></a> objects<a class="headerlink" href="#userstring-objects" title="Permalink to this headline">¶</a></h2>
<p>The class, <a class="reference internal" href="#collections.UserString" title="collections.UserString"><tt class="xref py py-class docutils literal"><span class="pre">UserString</span></tt></a> acts as a wrapper around string objects.
The need for this class has been partially supplanted by the ability to
subclass directly from <a class="reference internal" href="functions.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a>; however, this class can be easier
to work with because the underlying string is accessible as an
attribute.</p>
<dl class="class">
<dt id="collections.UserString">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">UserString</tt><big>(</big><span class="optional">[</span><em>sequence</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#collections.UserString" title="Permalink to this definition">¶</a></dt>
<dd><p>Class that simulates a string or a Unicode string object.  The instance&#8217;s
content is kept in a regular string object, which is accessible via the
<tt class="xref py py-attr docutils literal"><span class="pre">data</span></tt> attribute of <a class="reference internal" href="#collections.UserString" title="collections.UserString"><tt class="xref py py-class docutils literal"><span class="pre">UserString</span></tt></a> instances.  The instance&#8217;s
contents are initially set to a copy of <em>sequence</em>.  The <em>sequence</em> can
be an instance of <a class="reference internal" href="functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a>, <a class="reference internal" href="functions.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a>, <a class="reference internal" href="#collections.UserString" title="collections.UserString"><tt class="xref py py-class docutils literal"><span class="pre">UserString</span></tt></a> (or a
subclass) or an arbitrary sequence which can be converted into a string using
the built-in <a class="reference internal" href="functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> function.</p>
</dd></dl>

</div>
<div class="section" id="abcs-abstract-base-classes">
<span id="collections-abstract-base-classes"></span><h2>7.3.9. ABCs - abstract base classes<a class="headerlink" href="#abcs-abstract-base-classes" title="Permalink to this headline">¶</a></h2>
<p>The collections module offers the following <a class="reference internal" href="../glossary.html#term-abstract-base-class"><em class="xref std std-term">ABCs</em></a>:</p>
<table border="1" class="docutils">
<colgroup>
<col width="21%" />
<col width="18%" />
<col width="18%" />
<col width="43%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">ABC</th>
<th class="head">Inherits from</th>
<th class="head">Abstract Methods</th>
<th class="head">Mixin Methods</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><a class="reference internal" href="#collections.Container" title="collections.Container"><tt class="xref py py-class docutils literal"><span class="pre">Container</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__contains__</span></tt></td>
<td>&nbsp;</td>
</tr>
<tr><td><a class="reference internal" href="#collections.Hashable" title="collections.Hashable"><tt class="xref py py-class docutils literal"><span class="pre">Hashable</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__hash__</span></tt></td>
<td>&nbsp;</td>
</tr>
<tr><td><a class="reference internal" href="#collections.Iterable" title="collections.Iterable"><tt class="xref py py-class docutils literal"><span class="pre">Iterable</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__iter__</span></tt></td>
<td>&nbsp;</td>
</tr>
<tr><td><a class="reference internal" href="#collections.Iterator" title="collections.Iterator"><tt class="xref py py-class docutils literal"><span class="pre">Iterator</span></tt></a></td>
<td><a class="reference internal" href="#collections.Iterable" title="collections.Iterable"><tt class="xref py py-class docutils literal"><span class="pre">Iterable</span></tt></a></td>
<td><tt class="docutils literal"><span class="pre">__next__</span></tt></td>
<td><tt class="docutils literal"><span class="pre">__iter__</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.Sized" title="collections.Sized"><tt class="xref py py-class docutils literal"><span class="pre">Sized</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__len__</span></tt></td>
<td>&nbsp;</td>
</tr>
<tr><td><a class="reference internal" href="#collections.Callable" title="collections.Callable"><tt class="xref py py-class docutils literal"><span class="pre">Callable</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__call__</span></tt></td>
<td>&nbsp;</td>
</tr>
<tr><td><a class="reference internal" href="#collections.Sequence" title="collections.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a></td>
<td><a class="reference internal" href="#collections.Sized" title="collections.Sized"><tt class="xref py py-class docutils literal"><span class="pre">Sized</span></tt></a>,
<a class="reference internal" href="#collections.Iterable" title="collections.Iterable"><tt class="xref py py-class docutils literal"><span class="pre">Iterable</span></tt></a>,
<a class="reference internal" href="#collections.Container" title="collections.Container"><tt class="xref py py-class docutils literal"><span class="pre">Container</span></tt></a></td>
<td><tt class="docutils literal"><span class="pre">__getitem__</span></tt></td>
<td><tt class="docutils literal"><span class="pre">__contains__</span></tt>, <tt class="docutils literal"><span class="pre">__iter__</span></tt>, <tt class="docutils literal"><span class="pre">__reversed__</span></tt>,
<tt class="docutils literal"><span class="pre">index</span></tt>, and <tt class="docutils literal"><span class="pre">count</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.MutableSequence" title="collections.MutableSequence"><tt class="xref py py-class docutils literal"><span class="pre">MutableSequence</span></tt></a></td>
<td><a class="reference internal" href="#collections.Sequence" title="collections.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a></td>
<td><tt class="docutils literal"><span class="pre">__setitem__</span></tt>,
<tt class="docutils literal"><span class="pre">__delitem__</span></tt>,
<tt class="docutils literal"><span class="pre">insert</span></tt></td>
<td>Inherited <a class="reference internal" href="#collections.Sequence" title="collections.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a> methods and
<tt class="docutils literal"><span class="pre">append</span></tt>, <tt class="docutils literal"><span class="pre">reverse</span></tt>, <tt class="docutils literal"><span class="pre">extend</span></tt>, <tt class="docutils literal"><span class="pre">pop</span></tt>,
<tt class="docutils literal"><span class="pre">remove</span></tt>, and <tt class="docutils literal"><span class="pre">__iadd__</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.Set" title="collections.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a></td>
<td><a class="reference internal" href="#collections.Sized" title="collections.Sized"><tt class="xref py py-class docutils literal"><span class="pre">Sized</span></tt></a>,
<a class="reference internal" href="#collections.Iterable" title="collections.Iterable"><tt class="xref py py-class docutils literal"><span class="pre">Iterable</span></tt></a>,
<a class="reference internal" href="#collections.Container" title="collections.Container"><tt class="xref py py-class docutils literal"><span class="pre">Container</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__le__</span></tt>, <tt class="docutils literal"><span class="pre">__lt__</span></tt>, <tt class="docutils literal"><span class="pre">__eq__</span></tt>, <tt class="docutils literal"><span class="pre">__ne__</span></tt>,
<tt class="docutils literal"><span class="pre">__gt__</span></tt>, <tt class="docutils literal"><span class="pre">__ge__</span></tt>, <tt class="docutils literal"><span class="pre">__and__</span></tt>, <tt class="docutils literal"><span class="pre">__or__</span></tt>,
<tt class="docutils literal"><span class="pre">__sub__</span></tt>, <tt class="docutils literal"><span class="pre">__xor__</span></tt>, and <tt class="docutils literal"><span class="pre">isdisjoint</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.MutableSet" title="collections.MutableSet"><tt class="xref py py-class docutils literal"><span class="pre">MutableSet</span></tt></a></td>
<td><a class="reference internal" href="#collections.Set" title="collections.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a></td>
<td><tt class="docutils literal"><span class="pre">add</span></tt>,
<tt class="docutils literal"><span class="pre">discard</span></tt></td>
<td>Inherited <a class="reference internal" href="#collections.Set" title="collections.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a> methods and
<tt class="docutils literal"><span class="pre">clear</span></tt>, <tt class="docutils literal"><span class="pre">pop</span></tt>, <tt class="docutils literal"><span class="pre">remove</span></tt>, <tt class="docutils literal"><span class="pre">__ior__</span></tt>,
<tt class="docutils literal"><span class="pre">__iand__</span></tt>, <tt class="docutils literal"><span class="pre">__ixor__</span></tt>, and <tt class="docutils literal"><span class="pre">__isub__</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.Mapping" title="collections.Mapping"><tt class="xref py py-class docutils literal"><span class="pre">Mapping</span></tt></a></td>
<td><a class="reference internal" href="#collections.Sized" title="collections.Sized"><tt class="xref py py-class docutils literal"><span class="pre">Sized</span></tt></a>,
<a class="reference internal" href="#collections.Iterable" title="collections.Iterable"><tt class="xref py py-class docutils literal"><span class="pre">Iterable</span></tt></a>,
<a class="reference internal" href="#collections.Container" title="collections.Container"><tt class="xref py py-class docutils literal"><span class="pre">Container</span></tt></a></td>
<td><tt class="docutils literal"><span class="pre">__getitem__</span></tt></td>
<td><tt class="docutils literal"><span class="pre">__contains__</span></tt>, <tt class="docutils literal"><span class="pre">keys</span></tt>, <tt class="docutils literal"><span class="pre">items</span></tt>, <tt class="docutils literal"><span class="pre">values</span></tt>,
<tt class="docutils literal"><span class="pre">get</span></tt>, <tt class="docutils literal"><span class="pre">__eq__</span></tt>, and <tt class="docutils literal"><span class="pre">__ne__</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.MutableMapping" title="collections.MutableMapping"><tt class="xref py py-class docutils literal"><span class="pre">MutableMapping</span></tt></a></td>
<td><a class="reference internal" href="#collections.Mapping" title="collections.Mapping"><tt class="xref py py-class docutils literal"><span class="pre">Mapping</span></tt></a></td>
<td><tt class="docutils literal"><span class="pre">__setitem__</span></tt>,
<tt class="docutils literal"><span class="pre">__delitem__</span></tt></td>
<td>Inherited <a class="reference internal" href="#collections.Mapping" title="collections.Mapping"><tt class="xref py py-class docutils literal"><span class="pre">Mapping</span></tt></a> methods and
<tt class="docutils literal"><span class="pre">pop</span></tt>, <tt class="docutils literal"><span class="pre">popitem</span></tt>, <tt class="docutils literal"><span class="pre">clear</span></tt>, <tt class="docutils literal"><span class="pre">update</span></tt>,
and <tt class="docutils literal"><span class="pre">setdefault</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.MappingView" title="collections.MappingView"><tt class="xref py py-class docutils literal"><span class="pre">MappingView</span></tt></a></td>
<td><a class="reference internal" href="#collections.Sized" title="collections.Sized"><tt class="xref py py-class docutils literal"><span class="pre">Sized</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__len__</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.ItemsView" title="collections.ItemsView"><tt class="xref py py-class docutils literal"><span class="pre">ItemsView</span></tt></a></td>
<td><a class="reference internal" href="#collections.MappingView" title="collections.MappingView"><tt class="xref py py-class docutils literal"><span class="pre">MappingView</span></tt></a>,
<a class="reference internal" href="#collections.Set" title="collections.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__contains__</span></tt>,
<tt class="docutils literal"><span class="pre">__iter__</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.KeysView" title="collections.KeysView"><tt class="xref py py-class docutils literal"><span class="pre">KeysView</span></tt></a></td>
<td><a class="reference internal" href="#collections.MappingView" title="collections.MappingView"><tt class="xref py py-class docutils literal"><span class="pre">MappingView</span></tt></a>,
<a class="reference internal" href="#collections.Set" title="collections.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__contains__</span></tt>,
<tt class="docutils literal"><span class="pre">__iter__</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.ValuesView" title="collections.ValuesView"><tt class="xref py py-class docutils literal"><span class="pre">ValuesView</span></tt></a></td>
<td><a class="reference internal" href="#collections.MappingView" title="collections.MappingView"><tt class="xref py py-class docutils literal"><span class="pre">MappingView</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__contains__</span></tt>, <tt class="docutils literal"><span class="pre">__iter__</span></tt></td>
</tr>
</tbody>
</table>
<dl class="class">
<dt id="collections.Container">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">Container</tt><a class="headerlink" href="#collections.Container" title="Permalink to this definition">¶</a></dt>
<dt id="collections.Hashable">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">Hashable</tt><a class="headerlink" href="#collections.Hashable" title="Permalink to this definition">¶</a></dt>
<dt id="collections.Sized">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">Sized</tt><a class="headerlink" href="#collections.Sized" title="Permalink to this definition">¶</a></dt>
<dt id="collections.Callable">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">Callable</tt><a class="headerlink" href="#collections.Callable" title="Permalink to this definition">¶</a></dt>
<dd><p>ABCs for classes that provide respectively the methods <a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><tt class="xref py py-meth docutils literal"><span class="pre">__contains__()</span></tt></a>,
<a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><tt class="xref py py-meth docutils literal"><span class="pre">__hash__()</span></tt></a>, <a class="reference internal" href="../reference/datamodel.html#object.__len__" title="object.__len__"><tt class="xref py py-meth docutils literal"><span class="pre">__len__()</span></tt></a>, and <a class="reference internal" href="../reference/datamodel.html#object.__call__" title="object.__call__"><tt class="xref py py-meth docutils literal"><span class="pre">__call__()</span></tt></a>.</p>
</dd></dl>

<dl class="class">
<dt id="collections.Iterable">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">Iterable</tt><a class="headerlink" href="#collections.Iterable" title="Permalink to this definition">¶</a></dt>
<dd><p>ABC for classes that provide the <a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><tt class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></tt></a> method.
See also the definition of <a class="reference internal" href="../glossary.html#term-iterable"><em class="xref std std-term">iterable</em></a>.</p>
</dd></dl>

<dl class="class">
<dt id="collections.Iterator">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">Iterator</tt><a class="headerlink" href="#collections.Iterator" title="Permalink to this definition">¶</a></dt>
<dd><p>ABC for classes that provide the <a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><tt class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></tt></a> and <a class="reference internal" href="functions.html#next" title="next"><tt class="xref py py-meth docutils literal"><span class="pre">next()</span></tt></a> methods.
See also the definition of <a class="reference internal" href="../glossary.html#term-iterator"><em class="xref std std-term">iterator</em></a>.</p>
</dd></dl>

<dl class="class">
<dt id="collections.Sequence">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">Sequence</tt><a class="headerlink" href="#collections.Sequence" title="Permalink to this definition">¶</a></dt>
<dt id="collections.MutableSequence">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">MutableSequence</tt><a class="headerlink" href="#collections.MutableSequence" title="Permalink to this definition">¶</a></dt>
<dd><p>ABCs for read-only and mutable <a class="reference internal" href="../glossary.html#term-sequence"><em class="xref std std-term">sequences</em></a>.</p>
</dd></dl>

<dl class="class">
<dt id="collections.Set">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">Set</tt><a class="headerlink" href="#collections.Set" title="Permalink to this definition">¶</a></dt>
<dt id="collections.MutableSet">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">MutableSet</tt><a class="headerlink" href="#collections.MutableSet" title="Permalink to this definition">¶</a></dt>
<dd><p>ABCs for read-only and mutable sets.</p>
</dd></dl>

<dl class="class">
<dt id="collections.Mapping">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">Mapping</tt><a class="headerlink" href="#collections.Mapping" title="Permalink to this definition">¶</a></dt>
<dt id="collections.MutableMapping">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">MutableMapping</tt><a class="headerlink" href="#collections.MutableMapping" title="Permalink to this definition">¶</a></dt>
<dd><p>ABCs for read-only and mutable <a class="reference internal" href="../glossary.html#term-mapping"><em class="xref std std-term">mappings</em></a>.</p>
</dd></dl>

<dl class="class">
<dt id="collections.MappingView">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">MappingView</tt><a class="headerlink" href="#collections.MappingView" title="Permalink to this definition">¶</a></dt>
<dt id="collections.ItemsView">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">ItemsView</tt><a class="headerlink" href="#collections.ItemsView" title="Permalink to this definition">¶</a></dt>
<dt id="collections.KeysView">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">KeysView</tt><a class="headerlink" href="#collections.KeysView" title="Permalink to this definition">¶</a></dt>
<dt id="collections.ValuesView">
<em class="property">class </em><tt class="descclassname">collections.</tt><tt class="descname">ValuesView</tt><a class="headerlink" href="#collections.ValuesView" title="Permalink to this definition">¶</a></dt>
<dd><p>ABCs for mapping, items, keys, and values <a class="reference internal" href="../glossary.html#term-view"><em class="xref std std-term">views</em></a>.</p>
</dd></dl>

<p>These ABCs allow us to ask classes or instances if they provide
particular functionality, for example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">size</span> <span class="o">=</span> <span class="k">None</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">myvar</span><span class="p">,</span> <span class="n">collections</span><span class="o">.</span><span class="n">Sized</span><span class="p">):</span>
    <span class="n">size</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">myvar</span><span class="p">)</span>
</pre></div>
</div>
<p>Several of the ABCs are also useful as mixins that make it easier to develop
classes supporting container APIs.  For example, to write a class supporting
the full <a class="reference internal" href="#collections.Set" title="collections.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a> API, it only necessary to supply the three underlying
abstract methods: <a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><tt class="xref py py-meth docutils literal"><span class="pre">__contains__()</span></tt></a>, <a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><tt class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></tt></a>, and <a class="reference internal" href="../reference/datamodel.html#object.__len__" title="object.__len__"><tt class="xref py py-meth docutils literal"><span class="pre">__len__()</span></tt></a>.
The ABC supplies the remaining methods such as <a class="reference internal" href="../reference/datamodel.html#object.__and__" title="object.__and__"><tt class="xref py py-meth docutils literal"><span class="pre">__and__()</span></tt></a> and
<tt class="xref py py-meth docutils literal"><span class="pre">isdisjoint()</span></tt></p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">class</span> <span class="nc">ListBasedSet</span><span class="p">(</span><span class="n">collections</span><span class="o">.</span><span class="n">Set</span><span class="p">):</span>
     <span class="sd">&#39;&#39;&#39; Alternate set implementation favoring space over speed</span>
<span class="sd">         and not requiring the set elements to be hashable. &#39;&#39;&#39;</span>
     <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">iterable</span><span class="p">):</span>
         <span class="bp">self</span><span class="o">.</span><span class="n">elements</span> <span class="o">=</span> <span class="n">lst</span> <span class="o">=</span> <span class="p">[]</span>
         <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span>
             <span class="k">if</span> <span class="n">value</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">lst</span><span class="p">:</span>
                 <span class="n">lst</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
     <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
         <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">)</span>
     <span class="k">def</span> <span class="nf">__contains__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
         <span class="k">return</span> <span class="n">value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span>
     <span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
         <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">)</span>

<span class="n">s1</span> <span class="o">=</span> <span class="n">ListBasedSet</span><span class="p">(</span><span class="s">&#39;abcdef&#39;</span><span class="p">)</span>
<span class="n">s2</span> <span class="o">=</span> <span class="n">ListBasedSet</span><span class="p">(</span><span class="s">&#39;defghi&#39;</span><span class="p">)</span>
<span class="n">overlap</span> <span class="o">=</span> <span class="n">s1</span> <span class="o">&amp;</span> <span class="n">s2</span>            <span class="c"># The __and__() method is supported automatically</span>
</pre></div>
</div>
<p>Notes on using <a class="reference internal" href="#collections.Set" title="collections.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a> and <a class="reference internal" href="#collections.MutableSet" title="collections.MutableSet"><tt class="xref py py-class docutils literal"><span class="pre">MutableSet</span></tt></a> as a mixin:</p>
<ol class="arabic simple">
<li>Since some set operations create new sets, the default mixin methods need
a way to create new instances from an iterable. The class constructor is
assumed to have a signature in the form <tt class="docutils literal"><span class="pre">ClassName(iterable)</span></tt>.
That assumption is factored-out to an internal classmethod called
<tt class="xref py py-meth docutils literal"><span class="pre">_from_iterable()</span></tt> which calls <tt class="docutils literal"><span class="pre">cls(iterable)</span></tt> to produce a new set.
If the <a class="reference internal" href="#collections.Set" title="collections.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a> mixin is being used in a class with a different
constructor signature, you will need to override <tt class="xref py py-meth docutils literal"><span class="pre">_from_iterable()</span></tt>
with a classmethod that can construct new instances from
an iterable argument.</li>
<li>To override the comparisons (presumably for speed, as the
semantics are fixed), redefine <a class="reference internal" href="../reference/datamodel.html#object.__le__" title="object.__le__"><tt class="xref py py-meth docutils literal"><span class="pre">__le__()</span></tt></a> and
then the other operations will automatically follow suit.</li>
<li>The <a class="reference internal" href="#collections.Set" title="collections.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a> mixin provides a <tt class="xref py py-meth docutils literal"><span class="pre">_hash()</span></tt> method to compute a hash value
for the set; however, <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><tt class="xref py py-meth docutils literal"><span class="pre">__hash__()</span></tt></a> is not defined because not all sets
are hashable or immutable.  To add set hashabilty using mixins,
inherit from both <a class="reference internal" href="#collections.Set" title="collections.Set"><tt class="xref py py-meth docutils literal"><span class="pre">Set()</span></tt></a> and <a class="reference internal" href="#collections.Hashable" title="collections.Hashable"><tt class="xref py py-meth docutils literal"><span class="pre">Hashable()</span></tt></a>, then define
<tt class="docutils literal"><span class="pre">__hash__</span> <span class="pre">=</span> <span class="pre">Set._hash</span></tt>.</li>
</ol>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<ul class="last simple">
<li><a class="reference external" href="http://code.activestate.com/recipes/576694/">OrderedSet recipe</a> for an
example built on <a class="reference internal" href="#collections.MutableSet" title="collections.MutableSet"><tt class="xref py py-class docutils literal"><span class="pre">MutableSet</span></tt></a>.</li>
<li>For more about ABCs, see the <a class="reference internal" href="abc.html#module-abc" title="abc: Abstract base classes according to PEP 3119."><tt class="xref py py-mod docutils literal"><span class="pre">abc</span></tt></a> module and <span class="target" id="index-0"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3119"><strong>PEP 3119</strong></a>.</li>
</ul>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">7.3. <tt class="docutils literal"><span class="pre">collections</span></tt> &#8212; Container datatypes</a><ul>
<li><a class="reference internal" href="#counter-objects">7.3.1. <tt class="docutils literal"><span class="pre">Counter</span></tt> objects</a></li>
<li><a class="reference internal" href="#deque-objects">7.3.2. <tt class="docutils literal"><span class="pre">deque</span></tt> objects</a><ul>
<li><a class="reference internal" href="#deque-recipes">7.3.2.1. <tt class="docutils literal"><span class="pre">deque</span></tt> Recipes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#defaultdict-objects">7.3.3. <tt class="docutils literal"><span class="pre">defaultdict</span></tt> objects</a><ul>
<li><a class="reference internal" href="#defaultdict-examples">7.3.3.1. <tt class="docutils literal"><span class="pre">defaultdict</span></tt> Examples</a></li>
</ul>
</li>
<li><a class="reference internal" href="#namedtuple-factory-function-for-tuples-with-named-fields">7.3.4. <tt class="docutils literal"><span class="pre">namedtuple()</span></tt> Factory Function for Tuples with Named Fields</a></li>
<li><a class="reference internal" href="#ordereddict-objects">7.3.5. <tt class="docutils literal"><span class="pre">OrderedDict</span></tt> objects</a><ul>
<li><a class="reference internal" href="#ordereddict-examples-and-recipes">7.3.5.1. <tt class="docutils literal"><span class="pre">OrderedDict</span></tt> Examples and Recipes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#userdict-objects">7.3.6. <tt class="docutils literal"><span class="pre">UserDict</span></tt> objects</a></li>
<li><a class="reference internal" href="#userlist-objects">7.3.7. <tt class="docutils literal"><span class="pre">UserList</span></tt> objects</a></li>
<li><a class="reference internal" href="#userstring-objects">7.3.8. <tt class="docutils literal"><span class="pre">UserString</span></tt> objects</a></li>
<li><a class="reference internal" href="#abcs-abstract-base-classes">7.3.9. ABCs - abstract base classes</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="calendar.html"
                        title="previous chapter">7.2. <tt class="docutils literal docutils literal"><span class="pre">calendar</span></tt> &#8212; General calendar-related functions</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="heapq.html"
                        title="next chapter">7.4. <tt class="docutils literal docutils literal"><span class="pre">heapq</span></tt> &#8212; Heap queue algorithm</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/collections.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="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="heapq.html" title="7.4. heapq — Heap queue algorithm"
             >next</a> |</li>
        <li class="right" >
          <a href="calendar.html" title="7.2. calendar — General calendar-related functions"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="datatypes.html" >7. Data Types</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2011, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Sep 04, 2011.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>