Merge different key of single Dictionary to one key in Python

Viewed 58

I have a dictionary like:

d = {
  'S1': {
           'S11': {'first': 'a', 'second': 'b'},
           'S12': {'first': 'c', 'second': 'd'}
         },

   'S2': {
           'S21': {'first': 'l', 'second': 'e'},
           'S22': {'first': 'd', 'second': 't'}
          },
   'S3': {
           'S31': {'first': 'z', 'second': 'p'},
           'S32': {'first': 'x', 'second': 'g'}
          }

}

I want to merge this dict like:

{
 'S': {
           'S11': {'first': 'a', 'second': 'b'},
           'S12': {'first': 'c', 'second': 'd'},
           'S21': {'first': 'l', 'second': 'e'},
           'S22': {'first': 'd', 'second': 't'},
           'S31': {'first': 'z', 'second': 'p'},
           'S32': {'first': 'x', 'second': 'g'}

       }
}

The problem is in actual I have s1, s2, s3.....s100 and my current method is not very clear. Could someone please suggest a better way to achieve this?

Thanks

6 Answers

You can try the following:

d["S"] = {}
for i in d.keys():
    d["S"].update(d[i])

Which returns:

{'S11': {'first': 'a', 'second': 'b'}, 'S12': {'first': 'c', 'second': 'd'}, 'S21': {'first': 'l', 'second': 'e'}, 'S22': {'first': 'd', 'second': 't'}, 'S31': {'first': 'z', 'second': 'p'}, 'S32': {'first': 'x', 'second': 'g'}}

Use a ChainMap.

>>> from collections import ChainMap
>>> c = {'S': ChainMap(*d.values())}
>>> c['S']['S32']
{'first': 'x', 'second': 'g'}

You could convert the ChainMap back to a dict, if you like.

>>> c = {'S': dict(ChainMap(*d.values()))}
>>> c 
{'S': {
  'S31': {'first': 'z', 'second': 'p'},
  'S32': {'first': 'x', 'second': 'g'},
  'S21': {'first': 'l', 'second': 'e'},
  'S22': {'first': 'd', 'second': 't'},
  'S11': {'first': 'a', 'second': 'b'},
  'S12': {'first': 'c', 'second': 'd'}}
}

This can be done with a simple nested loop over the dict items.

new_d = {'S': {}}

for k, v in d.items():
    for k1, v1 in v.items():
        new_d['S'][k1] = v1

d['S']=new_d['S']
print(d['S'])

#Output
{'S': {'S11': {'first': 'a', 'second': 'b'}, 'S12': {'first': 'c', 'second': 'd'}, 'S21': {'first': 'l', 'second': 'e'}, 'S22': {'first': 'd', 'second': 't'}, 'S31': {'first': 'z', 'second': 'p'}, 'S32': {'first': 'x', 'second': 'g'}}}

Try this,

data = {}
for (k, v), j in d.items():
    if data.get(k):
        data[k].update(j)
    else:
        data[k] = j
from pprint import pprint as pp
d = {
   "S1": {"S11": {"first": "a", "second": "b"}, "S12": {"first": "c", 
"second": "d"}},
    "S2": {"S21": {"first": "l", "second": "e"}, "S22": {"first": "d", "second": "t"}},
    "S3": {"S31": {"first": "z", "second": "p"}, "S32": {"first": "x", "second": "g"}},
}

# Take out the values first
values = d.values()
# Use the values in the new dict
d_trans = {"S": values}
# Print the result
pp(d_trans)

You can simply do this in one line:-

res = {'S': {k1:v1 for v in d.values() for k1, v1 in v.items()}}

print(res)

Output:-

{
 'S': {
           'S11': {'first': 'a', 'second': 'b'},
           'S12': {'first': 'c', 'second': 'd'},
           'S21': {'first': 'l', 'second': 'e'},
           'S22': {'first': 'd', 'second': 't'},
           'S31': {'first': 'z', 'second': 'p'},
           'S32': {'first': 'x', 'second': 'g'}

       }
}
Related