How to define multidimensional dictionary with default value in python?
<p>I would like to modify next dict definition:</p>

<pre><code>class Vividict(dict):
      def __missing__(self, key):
           value = self[key] = type(self)()
           return value
</code></pre>

<p>To be able to use it in next way:</p>

<pre><code>totals[year][month] += amount    
</code></pre>

<p>Use <a href="https://docs.python.org/3/library/collections.html#collections.defaultdict" rel="nofollow"><code>collections.defaultdict</code></a> with <a href="https://docs.python.org/3/library/collections.html#collections.Counter" rel="nofollow"><code>collections.Counter</code></a>. </p>

<pre><code>from collections import defaultdict, Counter

d = defaultdict(Counter)
d['year']['month'] += 1
</code></pre>

python dictionary autovivification