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


Messages In This Thread
merging two dictionaries - by Skaperen - Oct-03-2017, 01:13 AM
RE: merging two dictionaries - by buran - Oct-03-2017, 01:33 AM
RE: merging two dictionaries - by buran - Oct-03-2017, 01:36 AM
RE: merging two dictionaries - by metulburr - Oct-03-2017, 01:34 AM
RE: merging two dictionaries - by Skaperen - Oct-03-2017, 01:57 AM
RE: merging two dictionaries - by buran - Oct-03-2017, 04:12 AM
RE: merging two dictionaries - by nilamo - Oct-03-2017, 05:21 AM
RE: merging two dictionaries - by Skaperen - Oct-03-2017, 08:57 AM
RE: merging two dictionaries - by wavic - Oct-03-2017, 09:25 AM
RE: merging two dictionaries - by snippsat - Oct-03-2017, 10:07 AM
RE: merging two dictionaries - by wavic - Oct-03-2017, 10:27 AM
RE: merging two dictionaries - by buran - Oct-03-2017, 10:29 AM
RE: merging two dictionaries - by snippsat - Oct-03-2017, 11:23 AM
RE: merging two dictionaries - by Skaperen - Oct-04-2017, 02:58 AM
RE: merging two dictionaries - by Skaperen - Oct-04-2017, 05:46 AM
RE: merging two dictionaries - by nilamo - Oct-04-2017, 03:05 PM
RE: merging two dictionaries - by Skaperen - Oct-05-2017, 12:47 AM
RE: merging two dictionaries - by DeaD_EyE - Oct-04-2017, 07:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  merging three dictionaries Skaperen 3 2,077 Oct-20-2020, 10:06 PM
Last Post: Skaperen
  Merging Dictionaries - Optimum Style? adt 5 3,111 Oct-09-2019, 05:26 PM
Last Post: adt
  merging dictionaries Skaperen 3 2,606 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