You can use nested-loop to go through your dictionary, and construct new one by updating key/values using setdefault.
d={ 'eth1': { 'R2': 'bw1', 'R3': 'bw3' }, 'eth2': { 'R2': 'bw2', 'R3': 'bw4' } } result = {} for k, v in d.iteritems(): for a,b in v.iteritems(): result.setdefault(a, {}).update({k:b}) print result
Output:
{'R2': {'eth2': 'bw2', 'eth1': 'bw1'}, 'R3': {'eth2': 'bw4', 'eth1': 'bw3'}}
You can use nested loops in list comprehensions to write smaller solution, and it would give the same result.
result = {} res= [result.setdefault(a, {}).update({k:b}) for k, v in d.iteritems() for a,b in v.iteritems()] print result #Output: {'R2': {'eth2': 'bw2', 'eth1': 'bw1'}, 'R3': {'eth2': 'bw4', 'eth1': 'bw3'}}