Python dictionary copying
Python Dictionary Copying
In Python, a dictionary is an unordered, mutable, and iterable data type used to store key-value pairs. Sometimes we need to copy a dictionary instead of referencing it so that we can modify the copy without affecting the original. This article details how to copy dictionaries in Python, including the concepts and specific usage of shallow and deep copies.
What is a Dictionary?
In Python, a dictionary is a data structure consisting of an unordered set of key-value pairs. Each key in a dictionary is unique, but values can be repeated. Dictionaries can be created using either the {} or dict() function. Here’s a simple dictionary example:
# Create a dictionary
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
print(person)
<p>Running result:
<pre><code class="language-python line-numbers">{'name': 'Alice', 'age': 25, 'city': 'New York'}
In the above example, we create a dictionary named person
, which contains three key-value pairs: name, age, and city.
Copying a Dictionary
In Python, there are two ways to copy a dictionary: shallow copying and deep copying. We’ll explain these two methods and their differences below.
Shallow Copy
A shallow copy creates a new dictionary, but the values in the new dictionary still refer to the objects in the original dictionary. This means that if the values in the original dictionary are mutable objects, modifying the values in the new dictionary will also affect the original dictionary.
You can use the copy()
method or the dict()
function to perform a shallow copy. The following is an example of a shallow copy:
# Create a dictionary
person = {
“name”: “Alice”,
“age”: 25,
“city”: “New York”
}
# Shallow copy
new_person = person.copy()
# Modify the value of the new dictionary
new_person[“age”] = 30
print(person)
print(new_person)
Running result:
{'name': 'Alice', 'age': 25, 'city': 'New York'}
{'name': 'Alice', 'age': 30, 'city': 'New York'}
As can be seen from the result, modifying the age
value in the new dictionary does not affect the value in the original dictionary.
Deep Copy
A deep copy creates a new dictionary, and the values in the new dictionary are also new object references. This way, even if the values in the original dictionary are mutable objects, modifying the values in the new dictionary will not affect the original.
You can use the copy.deepcopy()
method to perform a deep copy. Here’s an example of a deep copy:
import copy
# Create a dictionary
person = {
“name”: “Alice”,
“age”: 25,
“city”: [“New York”, “Los Angeles”]
}
# Deep copy
new_person = copy.deepcopy(person)
# Modify the list values in the new dictionary
new_person[“city”].append(“Chicago”)
print(person)
print(new_person)
Running result:
{‘name’: ‘Alice’, ‘age’: 25, ‘city’: [‘New York’, ‘Los Angeles’]}
{‘name’: ‘Alice’, ‘age’: 25, ‘city’: [‘New York’, ‘Los Angeles’, ‘Chicago’]}
As you can see from the results, modifying the values in the city
list in the new dictionary does not affect the values in the original dictionary.
Summary
In Python, dictionaries are a common data structure. We may copy dictionaries to perform operations on the copies when necessary. A shallow copy copies the object references in the dictionary, and modifications to mutable objects will affect the original dictionary. A deep copy, on the other hand, creates new object references, and modifications to mutable objects will not affect the original dictionary. Choosing the appropriate copying method based on your specific needs can ensure correct program operation.