Python Dictionary copy() Function
The Dictionary copy()
method returns the Shallow copy of the specified dictionary.
Syntax
my_dictionary.copy()
copy() Parameters
Python Dictionary copy()
function does not take any parameters.
copy() Return Value
Python Dictionary copy()
function returns the Shallow copy of the specified dictionary.
It does not modify the original dictionary.
Examples
Example 1: Copy a Dictionary with copy()
Consider this example in which:
original_dict.copy()
creates a shallow copy oforiginal_dict
.- When the age
key
incopied_dict
is modified, it does not affectoriginal_dict
because the values in the dictionaries are references to the same objects.
# create a dictionary
original_dict = {'name': 'Tom', 'age': 20}
# create a copy of the original_dict
copied_dict = original_dict.copy()
print("Original Dictionary:", original_dict)
print("Copied Dictionary:", copied_dict)
# Modifying the copied dictionary
copied_dict['age'] = 25
print("Original Dictionary after modification:", original_dict)
print("Copied Dictionary after modification:", copied_dict)
output
Original Dictionary: {'name': 'Tom', 'age': 20}
Copied Dictionary: {'name': 'Tom', 'age': 20}
Original Dictionary after modification: {'name': 'Tom', 'age': 20}
Copied Dictionary after modification: {'name': 'Tom', 'age': 25}
Example 2: Shallow Copy of a Dictionary with Mutable Values
The copy()
method creates a Shallow copy!
In the following example the values in original_dict
are lists (which are mutable) and so modifying one of the lists in copied_dict
also affects the corresponding list in original_dict
.
original_dict = {'fruits': ['apple', 'banana'], 'vegetables': ['carrot', 'tomato']}
copied_dict = original_dict.copy()
print("Original Dictionary:", original_dict)
print("Copied Dictionary:", copied_dict)
# Modifying the copied dictionary
copied_dict['fruits'].append('orange')
print("Original Dictionary after modification:", original_dict)
print("Copied Dictionary after modification:", copied_dict)
output
Original Dictionary: {'fruits': ['apple', 'banana'], 'vegetables': ['carrot', 'tomato']}
Copied Dictionary: {'fruits': ['apple', 'banana'], 'vegetables': ['carrot', 'tomato']}
Original Dictionary after modification: {'fruits': ['apple', 'banana', 'orange'], 'vegetables': ['carrot', 'tomato']}
Copied Dictionary after modification: {'fruits': ['apple', 'banana', 'orange'], 'vegetables': ['carrot', 'tomato']}
copy() vs Assignment statement
- When the
copy()
method is used, a new dictionary is created which is filled with a copy of the references from the original dictionary. - When the
=
operator is used, a new reference to the original dictionary is created.
For example, an assignment statement does not copy objects.
old_dict = {'name': 'Tom', 'age': 25}
new_dict = old_dict
new_dict['name'] = 'xx'
print(old_dict) # Output: {'age': 25, 'name': 'xx'}
print(new_dict) # Output: {'age': 25, 'name': 'xx'}
output
{'name': 'xx', 'age': 25}
{'name': 'xx', 'age': 25}
When you execute new_dict = old_dict
, you don’t actually have two dictionaries. The assignment just makes the two variables point to the one dictionary in memory.
So, when you change new_dict
, old_dict
is also modified. If you want to change one copy without changing the other, use copy()
method.
old_Dict = {'name': 'Tom', 'age': 25}
new_Dict = old_Dict.copy()
new_Dict['name'] = 'xx'
print(old_Dict) # Output: {'age': 25, 'name': 'Tom'}
print(new_Dict) # Output: {'age': 25, 'name': 'xx'}
output
{'name': 'Tom', 'age': 25}
{'name': 'xx', 'age': 25}
Equivalent Method: Dictionary Comprehension
An alternative way to copy a dictionary is using dictionary comprehension.
my_dictionary = {'name': 'Tom', 'age': 25}
new_dictionary = {k:v for k,v in my_dictionary.items()}
print(new_dictionary) # Output: {'age': 25, 'name': 'Tom'}
output
{'name': 'Tom', 'age': 25}