Python Dictionary copy() Method

The copy() method in Python is a built-in method used to create a shallow copy of a dictionary. A shallow copy of a dictionary means that a new dictionary is created, but the elements in the new dictionary are still references to the elements in the original dictionary.

Syntax

dict.copy()

Parameters

The copy() method does not take any parameters.

Examples

Example 1: Copying a Dictionary

Let’s consider a dictionary called student, and we will create a shallow copy of it using the copy() method:

student = {'name': 'John', 'age': 25, 'gender': 'Male'}
student_copy = student.copy()
print(student_copy)

In this example, we have created a dictionary called student, with key-value pairs representing the name, age, and gender of a student. Then, we have used the copy() method to create a shallow copy of the dictionary, which is stored in a new variable called student_copy. Finally, we have printed the new dictionary, which will be {'name': 'John', 'age': 25, 'gender': 'Male'}, the same as the original dictionary.

Example 2: Modifying a Shallow Copy

Let’s consider the following example, where we modify the shallow copy created in the previous example:

student = {'name': 'John', 'age': 25, 'gender': 'Male'}
student_copy = student.copy()
student_copy['age'] = 30
print(student)
print(student_copy)

In this example, we have created a dictionary called student, with key-value pairs representing the name, age, and gender of a student. Then, we have used the copy() method to create a shallow copy of the dictionary, which is stored in a new variable called student_copy. Then, we have modified the value of the 'age' key in the shallow copy to be 30. Finally, we have printed both dictionaries, which will be:

{'name': 'John', 'age': 25, 'gender': 'Male'}
{'name': 'John', 'age': 30, 'gender': 'Male'}

As we can see, modifying the shallow copy did not affect the original dictionary, but it did affect the shallow copy. This is because a shallow copy only creates a new reference to the elements in the original dictionary, not new copies of the elements.

Example 3: Copying a Nested Dictionary

Let’s consider a nested dictionary called student, and we will create a shallow copy of it using the copy() method:

student = {'name': 'John', 'age': 25, 'gender': 'Male', 'address': {'street': '123 Main St', 'city': 'San Francisco', 'state': 'CA'}}
student_copy = student.copy()
print(student_copy)

In this example, we have created a dictionary called student, with key-value pairs representing the name, age, and gender of a student, and an additional key called 'address' which has its own nested dictionary. Then, we have used the copy() method to create a shallow copy of the dictionary, which is stored in a new variable called student_copy. Finally, we have printed the new dictionary, which will be:

{'name': 'John', 'age': 25, 'gender': 'Male', 'address': {'street': '123 Main St', 'city': 'San Francisco', 'state': 'CA'}}

As we can see, the shallow copy of the nested dictionary is also a reference to the original nested dictionary, not a new copy. This means that if we modify the nested dictionary in the shallow copy, it will also affect the original nested dictionary.

For example, if we modify the street address in the shallow copy:

student_copy['address']['street'] = '456 Main St'
print(student)
print(student_copy)

Both dictionaries will be:

{'name': 'John', 'age': 25, 'gender': 'Male', 'address': {'street': '456 Main St', 'city': 'San Francisco', 'state': 'CA'}}
{'name': 'John', 'age': 25, 'gender': 'Male', 'address': {'street': '456 Main St', 'city': 'San Francisco', 'state': 'CA'}}

As we can see, the modification in the shallow copy also affected the original dictionary.

Therefore, if we want to create a completely independent copy of a nested dictionary, we need to use a deep copy, which can be achieved using the copy module in Python.

Conclusion

In conclusion, the copy() method in Python is a built-in method used to create a shallow copy of a dictionary. A shallow copy only creates a new reference to the elements in the original dictionary, not new copies of the elements. This method is useful when we want to create a separate reference to a dictionary without affecting the original dictionary, but it may not be the best option when dealing with nested dictionaries.

Leave a Reply

Your email address will not be published. Required fields are marked *