In this article, we will discuss the Python Dictionary copy() method. In addition to this, we will discuss the copy() method syntax, usage, and examples.
Dictionary copy() method in Python
Below are the topics that we are doing to discuss in this article:
- Introduction to Python Dictionary copy() method
- Syntax of the copy() method
- Limitations of the copy() method
- Examples of the copy() method
Python dictionary copy() method
Before diving into the Python dictionary copy() method, it’s important to understand the concept of shallow copies. A shallow copy is a new object that is a copy of the original object with a new reference.
It’s called “shallow” because it only creates a new reference to the original object, not the objects within the original object.
dictionary copy() method
The copy() method is a built-in method of the dictionary class in Python that allows us to create a shallow copy of a dictionary. The syntax is simple:
copied_dictionary = original_dictionary.copy()
copy() method in Python Dictionary Examples
Let’s dive into some examples to better understand the copy() method in action:
Example#1 Basic Usage
# Creating an original dictionary
original_dictionary = {"name": "John", "age": 30, "city": "New York"}
# Creating a shallow copy using the copy() method
copied_dictionary = original_dictionary.copy()
print("Original Dictionary:", original_dictionary)
print("Copied Dictionary:", copied_dictionary)
As we can see, the copy() method creates a new Python dictionary with the same key-value pairs as the original. However, since it’s a shallow copy, any modifications to the copied Python dictionary won’t affect the original dictionary, and vice versa.
Output:
Example#2 Shallow Copy with Nested Mutable Objects
# Importing the copy module for deep copy
import copy
# Creating an original dictionary with nested mutable objects
original_dictionary = {
"name": "Alice",
"age": 28,
"hobbies": ["reading", "travelling"],
"address": {"city": "San Francisco", "state": "California"}
}
# Creating a shallow copy using the copy() method
shallow_copied_dictionary = original_dictionary.copy()
# Modifying the copied dictionary
shallow_copied_dictionary["age"] = 29
shallow_copied_dictionary["hobbies"].append("cooking")
shallow_copied_dictionary["address"]["city"] = "Los Angeles"
print("Original Dictionary:", original_dictionary)
print("Shallow Copied Dictionary:", shallow_copied_dictionary)
- In this example, we create a Python dictionary that includes nested mutable objects. The dictionary contains keys such as
"name", "age", "hobbies", and "address"
. The value for"hobbies"
is a Python list (which is mutable), and the value for"address"
is another Python dictionary (also mutable). - We then create a shallow copy of the original Python dictionary using the copy() method. This creates a new Python dictionary that references the same objects as the original dictionary.
- When we modify the copied dictionary’s
"age"
key, it doesn’t affect the original Python dictionary because the value of"age"
is an integer, which is immutable. - However, when we modify the
"hobbies" and "address"
keys, it also affects the original Python dictionary. That’s because these keys point to mutable objects (a list and a dictionary), and both the original and copied Python dictionaries refer to the same objects.
Output:
So, we have concluded that the Python dictionary copy() method has limitation.
Limitations of the copy() Method
The main limitation of the Python dictionary copy() method is that it creates shallow copies. If our Python dictionary contains mutable objects, like lists or other dictionaries, the copy() method won’t create independent copies of those objects.
Instead, both the original and copied Python dictionaries will still reference the same mutable objects. So, to avoid this, we need to create a deep copy of the Python dictionary.
To create a deep copy, which creates independent copies of all objects within the original dictionary, we can use the Python copy module’s deepcopy() function.
import copy
deep_copied_dictionary = copy.deepcopy(original_dictionary)
Conclusion
The Python dictionary copy() method is a handy built-in function for creating shallow copies of dictionaries in Python. It allows us to create a new dictionary with the same key-value pairs as the original without affecting the original dictionary.
However, keep in mind that it only creates shallow copies, which can lead to unintended side effects when working with nested mutable Python objects.
You may also like to read the following articles:
- Python dictionary items() method [With Examples]
- How to create a dictionary in Python
- Python dictionary pop() method [With Examples]
- Python dictionary fromkeys() method [With Examples]
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.