Python Forum
Combining Dictionaries w/o overwriting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combining Dictionaries w/o overwriting
#1
Hello, I am trying to combine dictionaries into a single dictionary while retaining previous values.

I have provided an example code below. Ideally, I am trying to accomplish the following:

dict3 = {"dict1" = {"North America" : "United States" , "Europe" : "France" , "Asia" : "China" }, "dict2" = {"North America" : "Canada" , "Europe" : "Spain" , "Asia" : "Japan"}}

dict1 = {"North America" : "United States" , "Europe" : "France" , "Asia" : "China"}
dict2 = {"North America" : "Canada" , "Europe" : "Spain" , "Asia" : "Japan"}
dict3 = {}
dict3.update(dict1)
print(dict3)
dict3.update(dict2)
print(dict3)
I would be looking to add many potential dictionaries to the 'dict3' variable and all of the dictionaries could have "North America" as a key for example. Any feedback would be greatly appreciated!
Reply
#2
How do you want the dictionaries to combine when they have different information for the same key? Can you give an example of what the final dictionary should look like? Your example line above the code doesn't make sense to me.

You either have to keep track of separate dictionaries (perhaps contained in a list or a tuple), or you have to figure out what to do with conflicting keys.

list_of_dicts = [dict1, dict2]
Keeps all the information, it's just not a third dict.
Reply
#3
Sorry I meant to type : instead of the = sign.

So I want to dict3 to be the same as the targetdict. So effectively print(dict3) would be the same result as print(targetdict)

dict1 = {"North America" : "United States" , "Europe" : "France" , "Asia" : "China"}
dict2 = {"North America" : "Canada" , "Europe" : "Spain" , "Asia" : "Japan"}
dict3 = {}
targetdict = {"dict1" : {"North America" : "United States" , "Europe" : "France" , "Asia" : "China" }, "dict2" : {"North America" : "Canada" , "Europe" : "Spain" , "Asia" : "Japan"}}

dict3.update(dict1)
print(dict3)
dict3.update(dict2)
print(dict3)

If figured it out. The answer was actually very simple. Added below for reference-


dict1 = {"North America" : "United States" , "Europe" : "France" , "Asia" : "China"}
dict2 = {"North America" : "Canada" , "Europe" : "Spain" , "Asia" : "Japan"}
dict3 = {"North America" : "Mexico" , "Europe" : "Switzerland" , "Asia" : "Korea"}
finaldict = {}
targetdict = {"dict1" : {"North America" : "United States" , "Europe" : "France" , "Asia" : "China" }, "dict2" : {"North America" : "Canada" , "Europe" : "Spain" , "Asia" : "Japan"}}


finaldict['dict1'] = dict1
finaldict['dict2'] = dict2
finaldict['dict3'] = dict3

print(finaldict)
Reply
#4
That looks like it works. What is your question about it?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Requests Session Overwriting and cookie jar muzikman 0 1,290 Dec-13-2021, 02:22 PM
Last Post: muzikman
  combining 2 dictionaries Skaperen 6 2,130 Nov-01-2021, 05:04 PM
Last Post: Skaperen
  Python Paramiko mkdir command overwriting existing folder. How can i stop that? therenaydin 1 3,207 Aug-02-2020, 11:13 PM
Last Post: therenaydin
  AssertionError: View function mapping is overwriting an existing endpoint function Zhavi221 7 19,055 Apr-17-2019, 01:07 PM
Last Post: Zhavi221
  View function mapping is overwriting an existing endpoint function: index rarevesselt 12 19,449 Oct-22-2017, 12:34 AM
Last Post: rarevesselt

Forum Jump:

User Panel Messages

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