Python dictionary copy() Method
The Python dictionary copy() method is used to return a shallow copy of the current dictionary. A shallow copy of an object is one whose properties are identical to those of the source object from which the copy was made, sharing the same references (pointing to the same underlying values). In short, a new dictionary is created in which the references from the original dictionary is filled with a copy.
We can also copy the dictionary using the = operator, which points to the same object as the original. So, if any changes is made to the copied dictionary, it is also reflected in the original dictionary. In short, a new reference is created to the original dictionary.
Syntax
Following is the syntax of the Python dictionary copy() method −
dict.copy()
Parameters
This method does not accept any parameter.
Return Value
This method returns a shallow copy of the current dictionary.
Example
The following example shows the usage of Python dictionary copy() method. Here we are creating a dictionary 'dict_1', then creating a copy of it.
dict1 = {'Name': 'Zara', 'Age': 7};
dict2 = dict1.copy()
print ("New Dictionary : %s" % str(dict2))
When we run above program, it produces following result −
New Dictionary : {'Name': 'Zara', 'Age': 7}
Example
Now, we are updating the elements of the second dictionary.Thereafter, checking whether the changes are reflected in the first dictionary or not.
dictionary = {5: 'x', 15: 'y', 25: [7, 8, 9]}
print("The given dictionary is: ", dictionary)
# using copy() method to copy
d2 = dictionary.copy()
print("The new copied dictionary is: ", d2)
# Updating the elements in second dictionary
d2[15] = 2
# updating the items in the list
d2[25][1] = '98'
print("The updated dictionary is: ", d2)
Following is an output of the above code −
The given dictionary is: {5: 'x', 15: 'y', 25: [7, 8, 9]}
The new copied dictionary is: {5: 'x', 15: 'y', 25: [7, 8, 9]}
The updated dictionary is: {5: 'x', 15: 2, 25: [7, '98', 9]}
Example
In the code below we are creating a dictionary 'dict_1'. Then a shallow copy of the original dictionary is created. After that an element is added to the shallow copy. Thereafter, the result is retrieved showing that the element gets appended to the shallow copy of the dictionary while the original dictionary remains unchanged.
# Creating a dictionary
dict_1 = {"1": "Lion", "2": "Tiger"}
print("The first dictionary is: ", dict_1)
# Create a shallow copy of the first dictionary
SC = dict_1.copy()
print("The shallow copy of the dictionary is: ", SC)
# Append an element to the created shallow copy
SC[3] = "Cheetah"
print("The shallow copy after adding an element is: ", SC)
# No changes in the first dictionary
print("There is no changes in the first dictionary: ", dict_1)
Output of the above code is as follows −
The first dictionary is: {'1': 'Lion', '2': 'Tiger'}
The shallow copy of the dictionary is: {'1': 'Lion', '2': 'Tiger'}
The shallow copy after adding an element is: {'1': 'Lion', '2': 'Tiger', 3: 'Cheetah'}
There is no changes in the first dictionary: {'1': 'Lion', '2': 'Tiger'}