Python never implicitly copies objects. When you set dict2 = dict1, you are making them refer to the same exact dict object, so when you mutate it, all references to it keep referring to the object in its current state.
If you want to copy the dict (which is rare), you have to do so explicitly with
dict2 = dict(dict1)
or
dict2 = dict1.copy()
Note: If the values of your dict are mutable (list, dict, etc...). This solution may not work as expected because it is doing a shallow copy.
Answer from Mike Graham on Stack OverflowPython never implicitly copies objects. When you set dict2 = dict1, you are making them refer to the same exact dict object, so when you mutate it, all references to it keep referring to the object in its current state.
If you want to copy the dict (which is rare), you have to do so explicitly with
dict2 = dict(dict1)
or
dict2 = dict1.copy()
Note: If the values of your dict are mutable (list, dict, etc...). This solution may not work as expected because it is doing a shallow copy.
When you assign dict2 = dict1, you are not making a copy of dict1, it results in dict2 being just another name for dict1.
To copy the mutable types like dictionaries, use copy / deepcopy of the copy module.
import copy
dict2 = copy.deepcopy(dict1)
Duplicate a dictionary: dict2 = dict1.copy() vs dict2 = dict1
Copy dictionary and modify it without affecting the original
How do I make a copy of dict items without <built-in method copy of dict object at 0x1038eddc8>
Why doesn't dict.items() and dict.keys() return a list?
What is the benefit of these objects?
A list has to load all its data into memory before doing anything. These objects just reference the underlying dict, not duplicating anything. This means that they are less memory-hungry and that, if you are apt to exit the loop quickly, you didn't pay to copy everything beforehand.
To me they just seem to introduce unnecessary complexity to my code.
It is one more thing to keep in mind, and I share your skepticism about its value. Ultimately, few will share it.
FWIW, numpy.array does more guessing than your average nice function, and this leads to a lot of stuff like this. I'm not sure that there is a nice API it could provide that didn't have such hiccups sometimes, though.
I read somewhere that
FaveCar = {
'Brand': "Toyota",
'Model': "Celica",
'Year' : 2004
}
print("2004 FaveCar dict: ", FaveCar)
copied_FaveCar = FaveCar.copy() # <-- this creates a simple copy of the dictionary FaveCar like I'd expect
equated_to_FaveCar = FaveCar # <-- this creates a dictionary that changes whenever FaveCar changes
FaveCar = {
'Brand': "Subaru",
'Model': "Crosstrek",
'Year' : 2024
}
print("copied_FaveCar = FaveCar.copy()")
print("equated_to_FaveCar = FaveCar")
print("\n2024 FaveCar update:")
# However, I just tested it. What I said above does NOT seem to be true.
''' ------ output
2004 FaveCar dict: {'Brand': 'Toyota', 'Model': 'Celica', 'Year': 2004}
copied_FaveCar = FaveCar.copy()
equated_to_FaveCar = FaveCar
2024 FaveCar update:
FaveCar now is: {'Brand': 'Subaru', 'Model': 'Crosstrek', 'Year': 2024}
copied_FaveCar = {'Brand': 'Toyota', 'Model': 'Celica', 'Year': 2004}
equated_to_FaveCar = {'Brand': 'Toyota', 'Model': 'Celica', 'Year': 2004}
^-- equated_to_FaveCar did NOT magically change when I updated FaveCar
Was what I read dependent on a certain library being loaded?
I had google'd something like
"Why do I need the .copy() method when I can just equate 2 dictionaries?"
'''
I'm trying to build a new dictionary that is mutable from one that I want to remain unchanged to be used later.
Here is my loop, current_minor_power_dict = {} for x in min_power_dict: for y in arch['minor_p_list']: if x == y: current_minor_power_dict[x] = min_power_dict[x].copy
it works like I want BUT, the dictionary prints out as:
{'Damage_Field': <built-in method copy of dict object at 0x1038e8ab0>,'Explosion': <built-in method copy of dict object at 0x1038e8e58>, 'Flight': <built-in method copy of dict object at 0x1038e8ee8>, 'Force-Field': <built-in method copy of dict object at 0x1038e8f78>, 'Iron_Will': <built-in method copy of dict object at 0x1038ed168>, 'Resistance': <built-in method copy of dict object at 0x1038ed900>, 'Super-Strength': <built-in method copy of dict object at 0x1038eddc8>}
How do I make a clean copy of these dicts in my new dict please?