Open In App

Copy a Python Dictionary and only Edit the Copy

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Copying a dictionary in Python to preserve the original while making changes to the duplicate is a common task. Developers use methods like the `copy` module or the dictionary’s `copy()` method to create an independent duplicate, allowing for isolated edits. This article explores commonly used techniques to copy a dictionary and modify only the duplicate.

How To Copy A Dictionary And Only Edit The Copy

Below, are the examples of How To Copy A Dictionary And Only Edit The Copy in Python.

Copy A Dictionary And Only Edit The Copy Using copy() Method

In this example, below Python code creates two dictionaries, `test1` and `test2`, initially identical. The `copy()` method is used to duplicate `test1` into `test2`. After modifying the value associated with the key “2” in `test2` to “five”, the script prints both the initial and updated dictionaries.

Python3




test1 = {"1" : "one", "2" : "two", "3" : "three"}
  
test2 = test1.copy()
test2["2"] ="five"
  
# print initial dictionary
print("initial dictionary = ", test1)
  
# printing updated dictionary
print("updated dictionary = ", test2)


Output

initial dictionary =  {'1': 'one', '2': 'two', '3': 'three'}
updated dictionary =  {'1': 'one', '2': 'five', '3': 'three'}

Copy A Dictionary And Only Edit The Copy Using copy Module

In this example, below Python code utilizes the `deepcopy` function from the `copy` module to create a deep copy of the dictionary `test1` as `test2`. Subsequently, the value associated with the key “2” in `test2` is modified. The script then prints both the initial and updated dictionaries.

Python3




from copy import deepcopy
 
test1 =  {"1" : "one", "2" : "t0w", "3" : "three"}
test2 = deepcopy(test1)
test2["2"] = "two"
  
# print initial dictionary
print("initial dictionary = ", test1)
  
# printing updated dictionary
print("updated dictionary = ", test2)


Output

initial dictionary =  {'1': 'one', '2': 't0w', '3': 'three'}
updated dictionary =  {'1': 'one', '2': 'two', '3': 'three'}

Copy A Dictionary And Only Edit The Copy Using dict() Constructor

In this example, below Python code creates two dictionaries, `test1` and `test2`, initially identical. The `dict()` constructor is used to create a shallow copy of `test1` as `test2`. After updating the value associated with the key “2” in `test2` to “two”, the script prints both the initial and updated dictionaries.

Python3




test1 = {"1" : "one", "2" : "t0w", "3" : "three"}
test2 = dict(test1)
  
# updating test2
test2["2"] ="two"
  
# print initial dictionary
print("initial dictionary = ", test1)
  
# printing updated dictionary
print("updated dictionary = ", test2)


Output

initial dictionary =  {'1': 'one', '2': 't0w', '3': 'three'}
updated dictionary =  {'1': 'one', '2': 'two', '3': 'three'}

Copy A Dictionary And Only Edit The Copy Using Dictionary Comprehension

In this example, Python code employs dictionary comprehension to create a shallow copy of `test1` as `test2`. After modifying the value associated with the key “2” in `test2` to “five”, the script prints both the initial and updated dictionaries. The output illustrates that changes made to `test2` do not impact the original `test1` dictionary.

Python3




# Using dictionary comprehension
test1 = {"1": "one", "2": "two", "3": "three"}
test2 = {key: value for key, value in test1.items()}
test2["2"] = "five"
 
# Printing the initial dictionary 'test1'
print("Initial Dictionary:", test1)
 
# Printing the updated dictionary 'test2'
print("Updated Dictionary:", test2)


Output

Initial Dictionary: {'1': 'one', '2': 'two', '3': 'three'}
Updated Dictionary: {'1': 'one', '2': 'five', '3': 'three'}

Conclusion

In conclusion, this article delves into the significance of copying a dictionary and exclusively editing the duplicate to prevent inadvertent alterations to the original data in Python. By employing methods like `copy()`, `deepcopy()`, `dict()` constructor, and dictionary comprehension, the integrity of the original dictionary is maintained while allowing for independent modifications. The selection of the appropriate method hinges on the specific use case requirements, such as the necessity for a shallow or deep copy.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads