Python Forum
Merge two dict with same key
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Merge two dict with same key
#3
By unpacking each dictionary in that manner, you're essentially doing this:

{'a': 1, 'b': 2, 'b': 3, 'c': 4}
In that case, the interpreter is going to:
  1. Initialize the dict
  2. Set key "b" to 2
  3. Reset key "b" to 3

If you reverse the order of x and y, you'll get the opposite result:

x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
z = {**y, **x}
print(z)
Output:
{'b': 2, 'c': 4, 'a': 1}
Reply


Messages In This Thread
Merge two dict with same key - by RavCOder - Oct-10-2019, 03:19 PM
RE: Merge two dict with same key - by buran - Oct-10-2019, 03:24 PM
RE: Merge two dict with same key - by stullis - Oct-10-2019, 03:28 PM
RE: Merge two dict with same key - by RavCOder - Oct-10-2019, 03:56 PM

Forum Jump:

User Panel Messages

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