This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: dict.update does not concat/replace if same key
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Avnish Midha, eric.smith
Priority: normal Keywords:

Created on 2018-01-22 06:22 by Avnish Midha, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
dict_try.py Avnish Midha, 2018-01-22 06:22
Messages (2)
msg310397 - (view) Author: Avnish Midha (Avnish Midha) Date: 2018-01-22 06:22
The dict does not seem to work as one would expect a dictionary or a hashtable to work. 

dict.update(otherdict) just replaces the entries as per otherdict, removing items which were extra in dict too and just keeping items as per new data in otherdict only. Shouldn't this be actually about updating the dict with new/updated entries and not touching those entries which pre-existed?

Sample program below:
import pandas as pd

x = {'name': ['A', 'B', 'C'], 'col2': [3, 4,6]}
y = {'name': ['B', 'C', 'D', 'E'], 'col2': [1,2, 9, 10]}


print ("X = \n" + str(x))

print ("Y = \n" + str(y))

x.update(y)

print ("updated dict = \n"  + str(x) + "\n")

#####

Output:

X = 
{'name': ['A', 'B', 'C'], 'col2': [3, 4, 6]}
Y = 
{'name': ['B', 'C', 'D', 'E'], 'col2': [1, 2, 9, 10]}
updated dict = 
{'name': ['B', 'C', 'D', 'E'], 'col2': [1, 2, 9, 10]}


Expected value:
updated dict = 
{'name': ['A','B', 'C', 'D', 'E'], 'col2': [3, 1, 2, 9, 10]}


Somehow to do this basic hashtable or dictionary update kind of operation is too complex in python and update() should have actually functioned as expected above IMO.
msg310400 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-01-22 07:46
I'm sorry the name is confusing to you, but this behavior is correct, and how dict.update is defined:

https://docs.python.org/3.6/library/stdtypes.html#dict.update

As such, it cannot be changed.

If you want a function that merges values, you'll need to write it yourself.

Eric.
History
Date User Action Args
2022-04-11 14:58:56adminsetgithub: 76798
2018-01-22 07:46:05eric.smithsetstatus: open -> closed

nosy: + eric.smith
messages: + msg310400

resolution: not a bug
stage: resolved
2018-01-22 06:22:13Avnish Midhacreate