Python 3 – dictionary copy() Method
A dictionary is a fundamental data structure in Python that maps unique keys to values. The dictionary consists of key-value pairs, where each key is associated with a value. In this article, we will explore the Python dictionary copy()
method, which creates a shallow copy of the dictionary.
Overview of the dictionary copy()
method
Python provides the copy()
method that creates a shallow copy of the dictionary. The copy()
method returns a new dictionary with the same key-value pairs as the original dictionary. The new dictionary is independent of the original dictionary, meaning that changes made to the new dictionary do not affect the original dictionary, and vice versa.
The syntax for using the copy()
method is as follows:
new_dict = old_dict.copy()
Here, new_dict
is the new dictionary that is a shallow copy of the old_dict
dictionary.
Example usage of the dictionary copy()
method
To better understand the dictionary copy()
method, let’s look at some examples.
# define a dictionary
dict1 = {1: 'one', 2: 'two', 3: 'three'}
# create a copy of the dictionary
dict2 = dict1.copy()
# print the dictionaries
print("dict1:", dict1)
print("dict2:", dict2)
Output:
dict1: {1: 'one', 2: 'two', 3: 'three'}
dict2: {1: 'one', 2: 'two', 3: 'three'}
In the above example, we first define a dictionary dict1
with three key-value pairs. We then create a new dictionary dict2
by calling the copy()
method on dict1
. Finally, we print both dictionaries to verify that they are identical.
Let’s look at a slightly more complex example:
# define a dictionary of personal information
person = {'name': 'John', 'age': 27, 'address': {'street': '123 Main St', 'city': 'Anytown, USA'}}
# make a shallow copy of the dictionary
person_copy = person.copy()
# change the value of the age key in the copy
person_copy['age'] = 28
# change the value of the city key in the copy
person_copy['address']['city'] = 'Somewhere, USA'
# print both dictionaries
print("Original:", person)
print("Copy:", person_copy)
Output:
Original: {'name': 'John', 'age': 27, 'address': {'street': '123 Main St', 'city': 'Somewhere, USA'}}
Copy: {'name': 'John', 'age': 28, 'address': {'street': '123 Main St', 'city': 'Somewhere, USA'}}
In the above example, we define a dictionary person
that contains personal information about John. We then create a new dictionary person_copy
by calling the copy()
method on person
. We then modify two values in the person_copy
dictionary: we increment the age by one and change the city in the address. Lastly, we print both dictionaries to verify that the original dictionary is unchanged and the copy has the updated information.
Conclusion
The dictionary copy()
method is a must-know feature for any Python programmer who works with dictionaries. It is an efficient way to create a shallow copy of a dictionary, which is a new dictionary that contains the same key-value pairs as the original dictionary. This feature ensures that any changes made to the new dictionary do not impact the original dictionary.