Merge nested dictionaries, by nested keys?
Asked Answered
H

4

8

I have several dictionaries with different and common keys, plus different and common keys in the nested dictionary. Below is a simplified example, the actual dictionaries have thousands of keys.

{1:{"Title":"Chrome","Author":"Google","URL":"http://"}}
{1:{"Title":"Chrome","Author":"Google","Version":"7.0.577.0"}}
{2:{"Title":"Python","Version":"2.5"}}

Which I'd like to merge into a single dictionary.

{1:{"Title":"Chrome","Author":"Google","URL":"http://","Version":"7.0.577.0"},
 2:{"Title":"Python","Version":"2.5"}}

I can iterate over both dictionaries, compare keys and update the nested dictionaries, but there is probably a more efficient, or pythonic, way to do this. If not, which is the most efficient?

Values of the nested dictionary need not be compared.

Horseman answered 20/11, 2010 at 21:3 Comment(3)
If you're really mapping sequential integer keys, wouldn't it make more sense to produce a list as the output?Tinkle
Lots of discussion and tips here as well: #39487Cutshall
Seems very unrealistic that each of the "several dictionaries" has exactly one key/value pair in it -- makes one wonder why they're dictionaries at all when a simple tuple or list with two items in it would work just as well.Padding
S
6
from collections import defaultdict

mydicts = [
   {1:{"Title":"Chrome","Author":"Google","URL":"http://"}},
   {1:{"Title":"Chrome","Author":"Google","Version":"7.0.577.0"}},
   {2:{"Title":"Python","Version":"2.5"}},
]

result = defaultdict(dict)

for d in mydicts:
    for k, v in d.iteritems():
        result[k].update(v)

print result

defaultdict(<type 'dict'>, 
    {1: {'Version': '7.0.577.0', 'Title': 'Chrome', 
         'URL': 'http://', 'Author': 'Google'}, 
     2: {'Version': '2.5', 'Title': 'Python'}})
Shieh answered 20/11, 2010 at 21:18 Comment(3)
Minor tweak: The second for could be replaced with k,v = next(d.iteritems()) and the next line dedented. Not really much difference, but it would look faster. ;-)Padding
@martineau: it would also look less useful: next() only works on python 2.6+. Also the way I wrote accepts more than one key on each dict, or empty dict without choking or doing the wrong thingShieh
Well, you're correct about next() not being available prior to v2.6 -- when writing it I forgot the OP wanted a v2.5 compatible solution. However it's arguable whether accepting ill-formed nested dicts with more than one key would be desirable, and lastly, next() can handle empty dicts without a problem. All things considered, I still believe my comment is a valid observation to those out there using at least v2.6 of Python.Padding
T
2

From your example, looks like you can do something like:

from collections import defaultdict
mydict = defaultdict(dict)
for indict in listofdicts:
    k, v = indict.popitem()
    mydict[k].update(v)
Tinkle answered 20/11, 2010 at 21:19 Comment(0)
N
0

Try with a NestedDict. First install ndicts

pip install ndicts

Then

from ndicts.ndicts import NestedDict

my_dicts = [
   {1:{"Title":"Chrome","Author":"Google","URL":"http://"}},
   {1:{"Title":"Chrome","Author":"Google","Version":"7.0.577.0"}},
   {2:{"Title":"Python","Version":"2.5"}},
]
nd = NestedDict()

for my_dict in my_dicts:
    nd.update(NestedDict(my_dict))

To get the result as a dictionary

>>> nd.to_dict()
{1: {'Title': 'Chrome', 'Author': 'Google', 'Version': '7.0.577.0', 'URL': 'http://'}, 
 2: {'Title': 'Python', 'Version': '2.5'}}
Nethermost answered 7/3, 2022 at 18:41 Comment(0)
I
0

This approach is much faster

my_dicts = [   {1:{"Title":"Chrome","Author":"Google","URL":"http://"}},   {1:{"Title":"Chrome","Author":"Google","Version":"7.0.577.0"}},   {2:{"Title":"Python","Version":"2.5"}},]
result = {}
for d in my_dicts:
    for k, v in d.items():
        result.setdefault(k, {}).update(v)
print(result)
Isolt answered 15/2, 2023 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.