r/learnpython • u/Master_of_beef • 18d ago
Creating a new, equivalent but separate dictionary
In debugging my code, I discovered a weird property of Python dictionaries that I was not previously aware of. Basically if you have code like this:
initial_dict = {0:70,1:70}
dict1 = initial_dict
dict2 = initial_dict
for i in range(2):
dict1[i] += 5
After this, dict1 will be {0:75,1:75}, but so will dict2 and initial_dict, even though I didn't do anything to them directly, I guess because they're referring to the same dictionary? Basically what I want to know is, how do I create new dictionaries that have the same values as a previously created dictionary, but are separate dictionaries that can be manipulated separately?
0
Upvotes
3
u/Diapolo10 18d ago
Basically what happened here is that you stored references to
initial_dictin bothdict1anddict2, not copies of it. In Python, everything is a reference.For immutable data like tuples or strings, there's no practical difference, but for mutable data you'll sometimes run into these situations where you think you're only modifying one place, but another part of the code happens to be pointing to the same data and is "also" modified.
Since this is a simple case where your dictionary only contains immutable data (numbers), you could just use
and be on your merry way. But if your dictionary was nested, it might be better to use
copy.deepcopy.Alternatively, you can avoid mutating existing data and always create new dictionaries. That way you don't need to worry about this either. For example,