Dictionary manipulation
<p>I have a dictionary of dictionaries which dialed down a notch or two looks like this:</p>

<pre><code>a = {114907: {114905: 1.4351310915,
              114908: 0.84635577943,
              114861: 61.490648372},
     113820: {113826: 8.6999361654,
              113819: 1.1412795216,
              111068: 1.1964946282,
              117066: 1.5595617822,
              113822: 1.1958951003},
     114908: {114906: 1.279878388,
              114907: 0.77568252572,
              114862: 2.5412545474}
     }
</code></pre>

<p>The operation I wanna perform is as follows:</p>

<p>For every key of a:</p>

<ul>
<li>If its value (the innermost dictionary, e.g., <code>{114905: 1.435.., 114908: 0.846.., 114861: 61.490..}</code>) contains keys that are present as keys on the outermost one as well (in this case <code>114908</code>), replace them with the <code>k, v</code> values from the latter and remove it entirely.</li>
<li>Finally, convert the outermost key to a tuple containing both the original key and the key that was popped from the innermost dict.</li>
</ul>

<p>The desired output would be this:</p>

<pre><code>b = {(114907, 114908): {114905: 1.4351310915,
                        114906: 1.279878388,
                        114862: 2.5412545474,
                        114861: 61.490648372},
     113820: {113826: 8.6999361654,
              113819: 1.1412795216,
              111068: 1.1964946282,
              117066: 1.5595617822,
              113822: 1.1958951003}
     }
</code></pre>

<p>I really hope you got what I am trying to achieve here because this is not even describable.</p>

<p>This is what I have so far but it fails in several points and I am deeply convinced that I am going down the wrong road. Eventually I will get there but it would be the most inefficient thing ever coded.</p>

<pre><code>from copy import deepcopy

temp = deepcopy(a)
for item in temp:
    for subitems, values in temp[item].items():
        if values &lt; 1.0:
            for k, v in temp[subitems].items():
                if k != item:
                    a[item][k] = v
#                   a[item].pop(subitems)
for i in a:
    print(i, a[i])
#114908 {114905: 1.4351310915, 114906: 1.279878388, 114907: 0.77568252572, 114861: 61.490648372, 114862: 2.5412545474}
#114907 {114905: 1.4351310915, 114906: 1.279878388, 114908: 0.84635577943, 114861: 61.490648372, 114862: 2.5412545474}
#113820 {113826: 8.6999361654, 113819: 1.1412795216, 111068: 1.1964946282, 117066: 1.5595617822, 113822: 1.1958951003}
</code></pre>

<p>Side question, why does <code>pop</code> in dictionaries return the <code>value</code> only and not the <code>key: value</code> pair?</p>

<p>EDIT</p>

<p>An important detail which might make the thing easier is that another way to look for what has to be modified are the inner dict values. If they are below 1.0 their keys are bound to be keys of the outer dict as well.</p>

<p>This should work</p>

<pre><code>a = {114907: {114905: 1.4351310915,
              114908: 0.84635577943,
              114861: 61.490648372},
     113820: {113826: 8.6999361654,
              113819: 1.1412795216,
              111068: 1.1964946282,
              117066: 1.5595617822,
              113822: 1.1958951003},
     114908: {114906: 1.279878388,
              114907: 0.77568252572,
              114862: 2.5412545474}
     }

# Lets call the keys leaders and its value is a dict of
# keys ( call them members ) to floats.
# if a member is also a leader, then the two leaders combine.

leaders = set(a.keys())
leaders_to_members = { leader: set(member_dict.keys()) for leader, member_dict in a.items() }
seen_leaders =set()
b = {}
for leader, members in leaders_to_members.items():
    if leader in seen_leaders:
        continue

    members_as_leaders = members.intersection(leaders)
    members_as_leaders.add(leader)

    v = {}
    for member_leader in members_as_leaders:
        v.update(a[member_leader])

    seen_leaders.update(members_as_leaders)

    # if its just one element, you want it as the key directly
    b_key = tuple(members_as_leaders) if len(members_as_leaders) &gt; 1 else members_as_leaders.pop()
    # as per your output, you've removed the key to float value if it is a leader
    b_val = { k: float_val for k, float_val in v.items() if k not in members_as_leaders }
    b[b_key] = b_val

print(b)
</code></pre>

<p>Output</p>

<pre><code>{113820: {111068: 1.1964946282,
          113819: 1.1412795216,
          113822: 1.1958951003,
          113826: 8.6999361654,
          117066: 1.5595617822},
 (114907, 114908): {114861: 61.490648372,
                    114862: 2.5412545474,
                    114905: 1.4351310915,
                    114906: 1.279878388}}
</code></pre>

<blockquote>
  <p>The side question: why does pop in dictionaries return the value only and not the key: value pair?</p>
</blockquote>

<pre><code>&gt;&gt;&gt; a.pop()
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: pop expected at least 1 arguments, got 0

&gt;&gt;&gt; help(a.pop)
"""
Help on built-in function pop:

pop(...) method of builtins.dict instance
    D.pop(k[,d]) -&gt; v, remove specified key and return the corresponding value.
    If key is not found, d is returned if given, otherwise KeyError is raised
"""
</code></pre>

<p>As you can see, pop expects the key, so it can pop the value. Since you are required to give it the key, it doesn't have to return the key back to you.</p>

python python-3.x dictionary