Oct-03-2017, 01:13 AM
i am looking for a way to merge, combine, or add two dictionaries. i would expect any key(s) in both would be in the result with the value from a designated dictionary (either the 1st or the 2nd).
i initially expected something like this to work:
i initially expected something like this to work:
d1 = {1:'one',2:'two'} d2 = {2:'deux',3:'trois'} d3 = d1 + d2giving a result the same as:
d3 = {1:'one',2:'deux',3:'trois'}the closest thing i could find in the documentation was the
.update
method. but, .update
would modify its self dictionary in place and not return the result, so:d1 = {1:'one',2:'two'} d2 = {2:'deux',3:'trois'} d3 = d1.update(d2)would not work, requiring code like:
d1 = {1:'one',2:'two'} d2 = {2:'deux',3:'trois'} d3 = copy.copy(d1) d3.update(d2)but in something like function call arguments this just gets messy. any good ideas how to merge two dictionaries without creating an extra one in the code?