Python Copy Dictionary
Copy Dictionaries in Python
In Python, dictionaries are mutable data structures that store data in key-value pairs. Sometimes, you may want to create a copy of a dictionary without linking it to the original. This lesson will guide you on how to copy dictionaries effectively in Python using different methods, ensuring that the copied dictionary is independent of the original.
1. Why Copy a Dictionary?
When you copy a dictionary, it is important to understand that a direct assignment (e.g., dict2 = dict1
) does not create a new copy of the dictionary. Instead, it creates a reference to the original dictionary. Any changes made to one will affect the other.
Example: Direct Assignment Creates a Reference
python
1# Creating a dictionary
2original_dict = {
3 "brand": "Tesla",
4 "model": "Model 3",
5 "year": 2022
6}
7
8# Creating a reference to the original dictionary
9copied_dict = original_dict
10
11# Modifying the copied dictionary
12copied_dict["year"] = 2023
13
14print(original_dict) # Output: {'brand': 'Tesla', 'model': 'Model 3', 'year': 2023}
In this example, changing copied_dict
also changes original_dict
because both variables point to the same dictionary.
2. Copying a Dictionary Using the copy() Method
One of the simplest ways to create a copy of a dictionary is by using the built-in copy()
method. This method creates a shallow copy of the dictionary.
Example: Copying with the copy() Method
python
1# Creating a dictionary
2car_info = {
3 "brand": "Audi",
4 "model": "A4",
5 "year": 2021
6}
7
8# Making a copy of the dictionary
9car_copy = car_info.copy()
10
11print(car_copy) # Output: {'brand': 'Audi', 'model': 'A4', 'year': 2021}
3. Copying a Dictionary Using the dict() Function
Another method to copy a dictionary is by using the built-in dict()
function. This approach also creates a shallow copy.
Example: Copying with the dict() Function
python
1# Creating a dictionary
2laptop_info = {
3 "brand": "HP",
4 "model": "Pavilion",
5 "year": 2020
6}
7
8# Making a copy of the dictionary using dict()
9laptop_copy = dict(laptop_info)
10
11print(laptop_copy) # Output: {'brand': 'HP', 'model': 'Pavilion', 'year': 2020}
4. Deep Copying a Dictionary
For nested dictionaries, where you want to copy all levels of the dictionary, you will need to create a deep copy using the copy
module's deepcopy()
method. This ensures that all levels of the original dictionary are copied, rather than just creating references.
Example: Deep Copying a Nested Dictionary
python
1import copy
2
3# Creating a nested dictionary
4nested_dict = {
5 "brand": "Apple",
6 "products": {
7 "phone": "iPhone",
8 "laptop": "MacBook"
9 }
10}
11
12# Making a deep copy of the nested dictionary
13nested_copy = copy.deepcopy(nested_dict)
14
15# Modifying the copy
16nested_copy["products"]["phone"] = "iPhone 14"
17
18print(nested_dict) # Output: {'brand': 'Apple', 'products': {'phone': 'iPhone', 'laptop': 'MacBook'}}
19print(nested_copy) # Output: {'brand': 'Apple', 'products': {'phone': 'iPhone 14', 'laptop': 'MacBook'}}
In this example, modifying nested_copy
does not affect nested_dict
, demonstrating the advantage of deep copying for nested structures.