Open In App

Deep Copy of a Dictionary In Python

Last Updated : 28 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Deep Copying ensures that modifications made to one copy don’t inadvertently affect the other. This concept is particularly important when we deal with nested data structures within dictionaries because shallow copies only create new references to the nested objects, which leads to potential unintended consequences. In this article, we will explore different approaches to create a Deep Copy of a dictionary In Python.

Deep Copy of a Dict in Python

Below are the possible approaches to creating a Deep Copy of a dict In Python

  • Using dict() Constructor
  • Using copy Module
  • Using Dictionary Comprehension

Deep Copy Of A Dict Using dict() Constructor

The below approach code uses the dict() constructor to create a shallow copy of the original dictionary, allowing modifications to the original data to affect the copied dictionary (result).

Python3




# original dictionary
input_dict = {
    'website': 'GeeksforGeeks',
    'topics': ['Algorithms', 'DSA', 'Python', 'ML']
}
  
# deep copy using the dict() constructor
result = dict(input_dict)
  
# modifying the original data
input_dict['website'] = 'GFG'
  
# printing the original and deep copied data
print("Original Dictionary:")
print(input_dict)
  
print("\nDeep Copied Dictionary:")
print(result)


Output

Original Dictionary:
{'website': 'GFG', 'topics': ['Algorithms', 'DSA', 'Python', 'ML']}

Deep Copied Dictionary:
{'website': 'GeeksforGeeks', 'topics': ['Algorithms', 'DSA', 'Python', 'ML']}

Deep Copy Of A Dict Using copy Module

The below approach code uses copy.deepcopy from the copy module in Python to create an independent deep copy of a dictionary, making sure that modifications to the original dictionary do not affect the copied version. The resulting result dictionary retains the original state of the data.

Python3




import copy
  
# dictionary data
input_dict = {
    'website': 'GeeksforGeeks',
    'topics': ['Algorithms', 'DSA', 'Python', 'ML']
}
  
# using deepcopy from the copy module to create a deep copy
result = copy.deepcopy(input_dict)
  
# modifying the original data to demonstrate the independence of the deep copy
input_dict['website'] = 'GFG'
  
# printing the original and deep copied data
print("Original Dictionary:")
print(input_dict)
  
print("\nDeep Copied Dictionary:")
print(result)


Output

Original Dictionary:
{'website': 'GFG', 'topics': ['Algorithms', 'DSA', 'Python', 'ML']}

Deep Copied Dictionary:
{'website': 'GeeksforGeeks', 'topics': ['Algorithms', 'DSA', 'Python', 'ML']}

Deep Copy Of A Dict Using Dictionary Comprehension

The below approach code uses dictionary comprehension along with copy.deepcopy to create a deep copy of each key-value pair in the original dictionary. Modifications to the original dictionary (dict) do not impact the resulting result, demonstrating the preservation of the original state in the deep copy.

Python3




import copy
  
# original dictionary
dict = {
    'website': 'GeeksforGeeks',
    'topics': ['Algorithms', 'DSA', 'Python', 'ML']
}
  
# deep copy using dictionary comprehension
result = {key: copy.deepcopy(value) for key, value in dict.items()}
  
# modifying the original data to demonstrate the independence of the deep copy
dict['website'] = 'GFG'
  
# printing the original and deep copied data
print("Original Dictionary:")
print(dict)
  
print("\nDeep Copied Dictionary:")
print(result)


Output

Original Dictionary:
{'website': 'GFG', 'topics': ['Algorithms', 'DSA', 'Python', 'ML']}

Deep Copied Dictionary:
{'website': 'GeeksforGeeks', 'topics': ['Algorithms', 'DSA', 'Python', 'ML']}

Conclusion

In conclusion, when working with dictionaries in Python, creating deep copies is crucial for maintaining data integrity, especially with nested structures. The above approaches using the copy module, dictionary comprehension, the json module, and the dict() constructor offer various methods to achieve this. Choosing the appropriate method depends on the complexity of the data, with copy.deepcopy() and json being preferable for handling nested structures, ensuring modifications to one copy do not inadvertently impact another.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads