I am trying to combine two multi-element dictionaries but am running into a problem.

From the example below, what I expected was the dictionary x to contain all three unique combinations.

What seems to have happened is that the y dictionary overwrote the x dictionary.

Am I just using this wrong? Is there a elegant way to add two multi element dictionaies?
thanks!

>>> x=nested_dict()
>>> y=nested_dict()
>>> 
>>> x[1][1]=1
>>> x[1][2]=2
>>> y[1][3]=3
>>> x.update(y)
>>> print x
defaultdict(<class 'nested_dict.nested_dict'>, {1: defaultdict(<class 'nested_dict.nested_dict'>, {3: 3})})

This is correct, as you are updating x[1], that is y's key is also 1 and so overwrite's whatever is in x for that key. Change to
y[2][3]=3
or use
x[1].update(y[1])
and you should get what you expect.. If that's not satisfactory then you will have to write your own routine to update however you want.

Thanks!

the

x[1].update(y[1])

format should work.

If I walk through the keys of Y and use the same value for the key of X, that should let me merge two arbitrary two element dictionaries :)

Indeed it should, but test for "key in x" as well, or use a try/except, as you can never tell. Please mark this "solved" so no wastes their time on a solved thread.

Edit: I just did the following and it worked fine so you don't have to test for "key in x" as update takes care of that for you.

x = {}
x[1] = {}
x[1][1]=1
x[1][2]=2

y = {}
y[2] = {}
y[2][3]=3
x.update(y)
print x
#
# prints
{1: {1: 1, 2: 2}, 2: {3: 3}}

Marked as solved. Thanks for the help!

This is what I ended up with, works great :)

def merge(lib1,lib2):
    for item in lib2.keys():
        lib1[item].update(lib2[item])
    return lib1

This is what I ended up with, works great :)

def merge(lib1,lib2):
    for item in lib2.keys():
        lib1[item].update(lib2[item])
    return lib1

Mild caveat, for this to work lib1 and lib2 have to be of type collections.defaultdict(dict) , and each value has to be a dictionary too.

Here is a typical eaxmple ...

# merge two dictionaries and handle key collisions with 
# collections.defaultdict(dict)

import collections

def merge(lib1, lib2):
    for item in lib2.keys():
        lib1[item].update(lib2[item])
    return lib1

d1 = {'a':{1:11}, 'b':{2:22}, 'c':{3:33}}
d2 = {'d':{4:44}, 'c':{5:55}, 'e':{6:66}}

# mutliple values go into a dictionary
lib1 = collections.defaultdict(dict)
lib2 = collections.defaultdict(dict)

# convert each dict to a defaultdict
lib1.update(d1)
lib2.update(d2)

print(lib1)
print(lib2)
print('-'*60)
print( merge(lib1, lib2) )

"""my result (notice key 'c')-->
defaultdict(<type 'dict'>, {'a': {1: 11}, 'c': {3: 33}, 'b': {2: 22}})
defaultdict(<type 'dict'>, {'c': {5: 55}, 'e': {6: 66}, 'd': {4: 44}})
------------------------------------------------------------
defaultdict(<type 'dict'>, {'a': {1: 11}, 'c': {3: 33, 5: 55}, 
'b': {2: 22}, 'e': {6: 66}, 'd': {4: 44}})
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.