Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
merging two dictionaries
#1
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:

d1 = {1:'one',2:'two'}
d2 = {2:'deux',3:'trois'}
d3 = d1 + d2
giving 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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Doesn't this do what you want?
 
d3 = d1.copy().update(d2)
Reply
#3
Quote: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?
put it in a function and call the function in the other functions params
Recommended Tutorials:
Reply
#4
(Oct-03-2017, 01:33 AM)buran Wrote: Doesn't this do what you want?
 
d3 = d1.copy().update(d2)

d3 = d1.copy()
d3.update(d2)
Reply
#5
i am wanting to put it in a function call and not even have d3, like:

example_function(d1+d2)
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
https://bugs.python.org/issue6410
Well, support for dict.__add__ has been rejected long ago
Reply
#7
You can always roll your own.  Just because it isn't in dict doesn't mean you can't use things that waddle and quack like dicts...

class Wrapper(dict):
    def __add__(self, other):
        final = {}
        for obj in [self, other]:
            final.update(obj)
        return final

a = Wrapper({1: 2, 3: 4})
b = Wrapper({1: 3, 5: 6})
print(a+b)
# {1: 3, 3: 4, 5: 6}
Reply
#8
i was thinking maybe i'd have to roll my own.  i get that thought when i can't find it in the docs.  dict add is not hard.  pike has it (different terminology ... "mapping" is the direct term in pike).

so would dictmerge(a,b,...) be a good choice, or maybe dictmerge([a,b,...]), or maybe an implementation that can do it either way.  or would you rather see a different name?  dictjoin?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
Well, it's is a good idea in general to be created Merge class to handle all obj + obj statements for all of these types which don't have __add__ method into the type class definition.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
>>> d1 = {1:'one',2:'two'}
>>> d2 = {2:'deux',3:'trois'}
>>> d3 = {**d1, **d2}
>>> d3
{1: 'one', 2: 'deux', 3: 'trois'}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  merging three dictionaries Skaperen 3 1,992 Oct-20-2020, 10:06 PM
Last Post: Skaperen
  Merging Dictionaries - Optimum Style? adt 5 2,963 Oct-09-2019, 05:26 PM
Last Post: adt
  merging dictionaries Skaperen 3 2,487 Nov-13-2018, 06:26 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020