Avoiding key error storing values in nested dictionary (Python)
<h1>Introduction</h1>

<p>Following dictionary has three levels of keys and then a value.</p>

<pre><code>d = {
    1:{
        'A':{
            'i': 100,
            'ii': 200
            }, 
        'B':{
            'i': 300
            }
        }, 
    2:{
        'A':{
            'ii': 500
            }
        }
    }
</code></pre>

<p>Examples that need to be added. </p>

<pre><code>d[1]['B']['ii'] = 600      # OK
d[2]['C']['iii'] = 700     # Keyerror on 'C'
d[3]['D']['iv'] = 800      # Keyerror on 3
</code></pre>

<h1>Problem Statement</h1>

<p>I wanted to create code that would create the necessary nested keys and avoid any key errors.</p>

<h1>Solution 1</h1>

<p>The first solution I came up with, was:</p>

<pre><code>def NewEntry_1(d, lv1, lv2, lv3, value):
    if lv1 in d:
        if lv2 in d:
            d[lv1][lv2][lv3] = value
        else:
            d[lv1][lv2] = {lv3: value}
    else:
        d[lv1] = {lv2: {lv3: value}}
</code></pre>

<p>Seems legit, but embedding this in order pieces of code made it mind boggling. I explored Stackoverflow for other solutions and was reading on the get() and setdefault() functions.</p>

<h1>Solution 2</h1>

<p>There is plenty of material to find about get() and setdefault(), but not so much on nested dictionaries. Ultimately I was able to come up with:</p>

<pre><code>def NewEntry_2(d, lv1, lv2, lv3, value):
    return d.setdefault(lv1, {}).setdefault(lv2,{}).setdefault(lv3, value)
</code></pre>

<p>It is one line of code so it is not really necessary to make it a function. Easily modifiable to include operations:</p>

<pre><code>d[lv1][lv2][lv3] = d.setdefault(lv1, {}).setdefault(lv2,{}).setdefault(lv3, 0) + value
</code></pre>

<p>Seems perfect?</p>

<h1>Question</h1>

<p>When adding large quantities of entries and doing many modifications, is option 2 better than option 1? Or should I define function 1 and call it? The answers I'm looking should take into account speed and/or potential for errors.</p>

<h1>Examples</h1>

<pre><code>NewEntry_1(d, 1, 'B', 'ii', 600)
# output = {1: {'A': {'i': 100, 'ii': 200}, 'B': {'i': 300, 'ii': 600}}, 2: {'A': {'ii': 500}}}

NewEntry_1(d, 2, 'C', 'iii', 700)
# output = {1: {'A': {'i': 100, 'ii': 200}, 'B': {'i': 300, 'ii': 600}}, 2: {'A': {'ii': 500}, 'C': {'iii': 700}}}

NewEntry_1(d, 3, 'D', 'iv', 800)
# output = {1: {'A': {'i': 100, 'ii': 200}, 'B': {'i': 300, 'ii': 600}}, 2: {'A': {'ii': 500}, 'C': {'iii': 700}}, 3: {'D': {'iv': 800}}}
</code></pre>

<h1>More background</h1>

<p>I'm a business analyst exploring using Python for creating Graph DB that would help me with very specific analysis. The dictionary structure is used to story the influence one node has on one of its neighbors: </p>

<ul>
<li>lv1 is Node From</li>
<li>lv2 is Node To </li>
<li>lv3 is Iteration </li>
<li>value is Influence (in
%)</li>
</ul>

<p>In the first iteration Node 1 has direct influence on Node 2. In the second iteration Node 1 influences all the Nodes that Node 2 is influencing.</p>

<p>I'm aware of packages that can help me with it (networkx), but I'm trying to understand Python/GraphDB before I want to start using them.</p>