creating nested dicts in python 3
<p>I'm trying to create the following nested dict structure, <code>{0: {0: 1}, 1: {0: 1, 1: 1}}</code> with the following code:</p>

<pre><code>feats = {}
for i in range(2):
    feat = feats.get(i, {})
    for j in range(i+1):
        feat[j] = 1
</code></pre>

<p>but all I'm getting <code>feats = {}</code>. Why is that? Thanks.</p>

<p>First of all I ran your program and I printed feats, it printed{0: 1, 1: 1} and not {}</p>

<p>Second thing, what I can understand from your requirement is that, in a generic way, while looping through a range of numbers, you want to have that number as key and value as dictionary containing key value pairs, where number of keys are from 0 to that number and value as 1. Correct me if I am wrong.</p>

<p>So the correct program for this would be:</p>

<pre><code>feats = {} 
for i in range(2): 
   feats[i] = feats.get(i, {}) 
   for j in range(0,i+1): 
      feats[i][j] = 1
print feats 
</code></pre>

<p>You can replace 2 with any number in range and get your answer accordingly.</p>

python python-3.x dictionary