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.

  • If you're really mapping sequential integer keys, wouldn't it make more sense to produce a list as the output? – Thomas K Nov 20 '10 at 21:15
  • Lots of discussion and tips here as well: http://stackoverflow.com/questions/38987/how-can-i-merge-two-python-dictionaries-as-a-single-expression – mjhm Nov 20 '10 at 21:20
  • 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. – martineau Nov 21 '10 at 13:35

4 Answers4

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'}})
nosklo
  • 217,122
  • 57
  • 293
  • 297
  • 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. ;-) – martineau Nov 21 '10 at 13:27
  • @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 thing – nosklo Dec 07 '10 at 10:51
  • 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 `dict`s with more than one key would be desirable, and lastly, `next()` can handle empty `dict`s 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. – martineau Dec 07 '10 at 18:43
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)
Thomas K
  • 39,200
  • 7
  • 84
  • 86
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'}}
edd313
  • 1,109
  • 7
  • 20
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)
abduljalil
  • 115
  • 1
  • 2