Python -Copy Dictionaries
Copy a Dictionary
You cannot copy a dictionary simply by typingdict2 = dict1, because:dict2 will only be areference todict1, and changes made indict1 will automatically also be made indict2.
There are ways to make a copy, one way is to use the built-in Dictionary methodcopy().
Example
Make a copy of a dictionary with thecopy() method:
thisdict ={
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = thisdict.copy()
print(mydict)
Try it Yourself »"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = thisdict.copy()
print(mydict)
Another way to make a copy is to use the built-in functiondict().
Example
Make a copy of a dictionary with thedict() function:
thisdict ={
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = dict(thisdict)
print(mydict)
Try it Yourself »"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = dict(thisdict)
print(mydict)

