If you copy a dictionary incorrectly, changes in one dictionary may affect the other.
Deep Copy :
Here , the copied dictionary variable refer to the same dictionary. Modifing one dictionary will affect the other.
Python
a = {"name": "Rahul"} b = a # not a real copy b["name"] = "Arun" print(a) #output {'name': 'Arun'} Shallow Copy
Here , both variable will point to different object . So changes made in original will not affect the duplicate.
copy( ) method is used to create a copy of original dictionary
Python
student = {"name": "Rahul", "age": 22} new_dict = student.copy() print(new_dict) new_dict["age"] = 25 print(student) print(new_dict) #output {'name': 'Rahul', 'age': 22} {'name': 'Rahul', 'age': 25}using dict( ) method :
built-in method to make copy of a dictionary
Python
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } mydict = dict(thisdict) print(mydict) #output