Build a dictionary using select values from a different dictionary
<p>I have a dictionary with years as keys and other dictionaries as values (these inner dictionaries contain tuples (i, i+1) as keys). An example of the format would be:</p>

<pre><code>myDict = {2000: {(0,1):111.1, (1,2):222.2, (2,3):333.3, (3,4):444.4}
          2001: {(0,1):11.1, (1,2):22.2, (2,3):33.3, (3,4):44.4}}
</code></pre>

<p>From this dictionary, I am trying to compile a dictionary, <code>secondDict</code>, that also has years for keys. The values will be the sum of the innermost values in <code>myDict</code> for only certain tuples (i.e. any tuple in that year whose 0th index is greater than 1). What I am going for is a dictionary that looks like this:</p>

<pre><code>secondDict = {2000: 777.7, 2001: 77.7}
</code></pre>

<p>The value in <code>secondDict</code> would be the sum of the values in <code>myDict[2000][tuple]</code> if the 1st number in the tuple was greater than or equal to <code>2</code>.</p>

<p>So far I have:</p>

<pre><code>years = [2000, 2001, 2002, 2003, 2011, 2012, 2013, 2014]
tuples = [(i, i+1) for i in range(65)]

for year in years:
    for key in myDict[year]:
        for value in myDict[year][key]:
            if key[0] &gt;= 30:
                secondDict[year] += value
</code></pre>

<p>I have several problems with my method here, but can't think of another way to build the dictionary.</p>

<p>1) Firstly, I'm getting a <code>TypeError: 'float' object is not iterable</code> for the third line of the loop (<code>for value in</code> ...). All the values that I am trying to access are floats, so I'm not sure how to work around this.</p>

<p>2) Moving on to problems I anticipate but haven't been able to address due to the <code>TypeError</code>: In the line <code>if key[0] &gt;= 30</code> I am trying to access the 0th index of the tuple; will this work / if not, how do I access it?</p>

<p>3) I am working with some fairly large dictionaries here and it seems like the running time for this many loops will be quite slow; however I'm pretty new to coding so my grasp of this is limited. Is it just <strong>O(n)</strong> for each loop, i.e. <strong>O(n^4)</strong> since there are four loops? How can I create a better, quicker algorithm to build a dictionary like this?</p>

<p><em><strong>EDIT:</strong></em></p>

<p>After more research and some finicking with the code, I now have:</p>

<pre><code>for year in years:
    for key in myDict[year].keys():
        if key[0] &gt;= 30:
            secondDict[year] += myDict[year][key]
</code></pre>

<p>This doesn't raise any errors, but upon printing it I find that it only compiles for one year:</p>

<pre><code>In[5]: secondDict
Out[5]: 
defaultdict(None,
            {2000: 0,
             2001: 0,
             2002: 0,
             2003: 27162828.602349777,
             2011: 0,
             2012: 0,
             2013: 0,
             2014: 0})
</code></pre>

<p>Why is it not iterating fully over <code>years</code>? Any suggestions?</p>

<p>Here's a dict comprehension that performs the action you specify at the start of your question, i.e., the values are the sum of the innermost values in <code>myDict</code> for tuples whose 0th index is greater than 1. </p>

<pre><code>myDict = {
    2000: {(0,1):111.1, (1,2):222.2, (2,3):333.3, (3,4):444.4},
    2001: {(0,1):11.1, (1,2):22.2, (2,3):33.3, (3,4):44.4},
}

secondDict = {y: sum(v for t, v in d.items() if t[0] &gt; 1) 
    for y, d in myDict.items()}
print(secondDict)
</code></pre>

<p><strong>output</strong></p>

<pre><code>{2000: 777.7, 2001: 77.69999999999999}
</code></pre>

python dictionary iteration