title: What is the purpose of collections.ChainMap?
body: <p>In Python 3.3 a <a href="https://docs.python.org/3/library/collections.html#collections.ChainMap"><code>ChainMap</code></a> class was added to the <a href="https://docs.python.org/3/library/collections.html"><code>collections</code></a> module:</p>
  
  <blockquote>
    <p>A ChainMap class is provided for quickly linking a number of mappings
    so they can be treated as a single unit. It is often much faster than
    creating a new dictionary and running multiple update() calls.</p>
  </blockquote>
  
  <p>Example:</p>
  
  <pre><code>&gt;&gt;&gt; from collections import ChainMap
  &gt;&gt;&gt; x = {'a': 1, 'b': 2}
  &gt;&gt;&gt; y = {'b': 10, 'c': 11}
  &gt;&gt;&gt; z = ChainMap(y, x)
  &gt;&gt;&gt; for k, v in z.items():
          print(k, v)
  a 1
  c 11
  b 10
  </code></pre>
  
  <p>It was motivated by <a href="http://bugs.python.org/issue11089">this issue</a> and made public by <a href="http://bugs.python.org/issue11297">this one</a> (no <code>PEP</code> was created).</p>
  
  <p>As far as I understand, it is an alternative to having an extra dictionary and maintaining it with <a href="https://docs.python.org/3/library/stdtypes.html#dict.update"><code>update()</code></a>s.</p>
  
  <p>The questions are:</p>
  
  <ul>
  <li>What use cases does <code>ChainMap</code> cover?</li>
  <li>Are there any real world examples of <code>ChainMap</code>? </li>
  <li>Is it used in third-party libraries that switched to python3?</li>
  </ul>
  
  <p>Bonus question: is there a way to use it on Python2.x?</p>
  
  <hr>
  
  <p>I've heard about it in <a href="https://www.youtube.com/watch?v=OSGv2VnC0go"><code>Transforming Code into Beautiful, Idiomatic Python</code></a> PyCon talk by Raymond Hettinger and I'd like to add it to my toolkit, but I lack in understanding when should I use it. </p>