I am doing some very basic operations with Python (3.6, but also tested in Python 2.11 - behavior is the same) and dictionaries. Here is the code sample:
max_values = dict()
min_values = dict()
initial_values = {"stat": 2, "value": 5.5}
max_values["zzz"] = initial_values
min_values["zzz"] = initial_values
So far so good, when I print those dictionaries:
for key, value in max_values.items():
print("[%s] Key [%s] -> Value [%s]" % ("max_values", key, value))
for key, value in min_values.items():
print("[%s] Key [%s] -> Value [%s]" % ("min_values", key, value))
I get my expected results:
[max_values] Key [zzz] -> Value [{'stat': 2, 'value': 5.5}]
[min_values] Key [zzz] -> Value [{'stat': 2, 'value': 5.5}]
But if I attempt to modify with the values, for example:
min_values["zzz"]["value"] = 1.0
And do the same print again, I get:
[max_values] Key [zzz] -> Value [{'stat': 2, 'value': 1.0}]
[min_values] Key [zzz] -> Value [{'stat': 2, 'value': 1.0}]
Question is: Why was the max_values
dictionary changed? I have only modified min_values
, but this new key assignment for min_value
dictionary also modified max_values
. Am I doing something wrong?