PYTHON DICTIONARY DEEP COPY OPERATION
INTRODUCTION
In deep copy operation, changes in original dictionary does not have the impact on copied dictionary and vice-versa.
There are four ways we can create deep copy in Python.
- Dictionary Slicing Operation importing itertools module
- Importing copy Module to use deepcopy() method
- Importing copy Module to use copy() method for Primitive Data Types
- Built-in copy() Method for Primitive Data Types
Dictionary Slicing Operation importing itertools module
# PYTHON DICTIONARY DEEP COPY OPERATION
# Example 1
import itertools
dict_class_wise_count_orig = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500}
print("\nList of key value pair of dict_class_wise_count_orig ->", dict_class_wise_count_orig)
dict_class_wise_count_copy = dict(itertools.islice(dict_class_wise_count_orig.items(), 5))
print("\nList of key value pair of dict_class_wise_count_copy ->", dict_class_wise_count_copy)
dict_class_wise_count_orig.pop('E')
dict_class_wise_count_copy['F'] = 600
print("\nList of key value pair of dict_class_wise_count_orig ->", dict_class_wise_count_orig)
print("\nList of key value pair of dict_class_wise_count_copy ->", dict_class_wise_count_copy)
PYTHON DICTIONARY DEEP COPY OPERATION : Output
List of key value pair of dict_class_wise_count_orig -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}
List of key value pair of dict_class_wise_count_copy -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}
List of key value pair of dict_class_wise_count_orig -> {'A': 100, 'B': 200, 'C': 300, 'D': 400}
List of key value pair of dict_class_wise_count_copy -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500, 'F': 600}
In the above example, we have used the dictionary slicing operation by importing itertools module and have created the copied dictionary. Then we have removed the last key “E” from original dictionary and added a new key in the copied dictionary. Both do not have the impact of one on another. So, this is an example of Deep copy.
Importing copy Module to use deepcopy() method
# PYTHON DICTIONARY DEEP COPY OPERATION
# Example 2
import copy
dict_class_wise_count_orig = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500}
print("\nList of key value pair of dict_class_wise_count_orig ->", dict_class_wise_count_orig)
dict_class_wise_count_copy = copy.deepcopy(dict_class_wise_count_orig)
print("\nList of key value pair of dict_class_wise_count_copy ->", dict_class_wise_count_copy)
dict_class_wise_count_orig.pop('E')
dict_class_wise_count_copy['F'] = 600
print("\nList of key value pair of dict_class_wise_count_orig ->", dict_class_wise_count_orig)
print("\nList of key value pair of dict_class_wise_count_copy ->", dict_class_wise_count_copy)
PYTHON DICTIONARY DEEP COPY OPERATION : Output
List of key value pair of dict_class_wise_count_orig -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}
List of key value pair of dict_class_wise_count_copy -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}
List of key value pair of dict_class_wise_count_orig -> {'A': 100, 'B': 200, 'C': 300, 'D': 400}
List of key value pair of dict_class_wise_count_copy -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500, 'F': 600}
In the above example, we have used the deepcopy() method by importing copy module and have created the copied dictionary. Then we have removed the last key “E” from original dictionary and added a new key in the copied dictionary. Both do not have the impact of one on another. So, this is an example of Deep copy.
# PYTHON DICTIONARY DEEP COPY OPERATION
# Example 3
import copy
int_list = [500, 501, 502]
dict_class_wise_count_orig = {'A':100, 'B':200, 'C':300, 'D':400, 'E':int_list}
print("\nList of key value pair of dict_class_wise_count_orig ->", dict_class_wise_count_orig)
dict_class_wise_count_copy = copy.deepcopy(dict_class_wise_count_orig)
print("\nList of key value pair of dict_class_wise_count_copy ->", dict_class_wise_count_copy)
int_list.append(503)
print("\nList of key value pair of dict_class_wise_count_orig ->", dict_class_wise_count_orig)
print("\nList of key value pair of dict_class_wise_count_copy ->", dict_class_wise_count_copy)
PYTHON DICTIONARY DEEP COPY OPERATION : Output
List of key value pair of dict_class_wise_count_orig -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': [500, 501, 502]}
List of key value pair of dict_class_wise_count_copy -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': [500, 501, 502]}
List of key value pair of dict_class_wise_count_orig -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': [500, 501, 502, 503]}
List of key value pair of dict_class_wise_count_copy -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': [500, 501, 502]}
In the above example, we have used the complex data type list in the original dictionary. But when we have modified the list item then original dictionary has been modified but the copied dictionary was unchanged. So deepcopy() method behaves like deep copy, both for primitive and complex data types.
Importing copy Module to use copy() method for Primitive Data Types
# PYTHON DICTIONARY DEEP COPY OPERATION
# Example 4
import copy
dict_class_wise_count_orig = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500}
print("\nList of key value pair of dict_class_wise_count_orig ->", dict_class_wise_count_orig)
dict_class_wise_count_copy = copy.copy(dict_class_wise_count_orig)
print("\nList of key value pair of dict_class_wise_count_copy ->", dict_class_wise_count_copy)
dict_class_wise_count_orig.pop('E')
dict_class_wise_count_copy['F'] = 600
print("\nList of key value pair of dict_class_wise_count_orig ->", dict_class_wise_count_orig)
print("\nList of key value pair of dict_class_wise_count_copy ->", dict_class_wise_count_copy)
PYTHON DICTIONARY DEEP COPY OPERATION : Output
List of key value pair of dict_class_wise_count_orig -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}
List of key value pair of dict_class_wise_count_copy -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}
List of key value pair of dict_class_wise_count_orig -> {'A': 100, 'B': 200, 'C': 300, 'D': 400}
List of key value pair of dict_class_wise_count_copy -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500, 'F': 600}
In the above example, we have used the copy() method by importing copy module and have created the copied dictionary. Then we have removed the last key “E” from original dictionary and added a new key in the copied dictionary. Both do not have the impact of one on another. So, this is an example of Deep copy.
Built-in copy() Method for Primitive Data Types
# PYTHON DICTIONARY DEEP COPY OPERATION
# Example 5
dict_class_wise_count_orig = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500}
print("\nList of key value pair of dict_class_wise_count_orig ->", dict_class_wise_count_orig)
dict_class_wise_count_copy = dict_class_wise_count_orig.copy()
print("\nList of key value pair of dict_class_wise_count_copy ->", dict_class_wise_count_copy)
dict_class_wise_count_orig.pop('E')
dict_class_wise_count_copy['F'] = 600
print("\nList of key value pair of dict_class_wise_count_orig ->", dict_class_wise_count_orig)
print("\nList of key value pair of dict_class_wise_count_copy ->", dict_class_wise_count_copy)
PYTHON DICTIONARY DEEP COPY OPERATION : Output
List of key value pair of dict_class_wise_count_orig -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}
List of key value pair of dict_class_wise_count_copy -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}
List of key value pair of dict_class_wise_count_orig -> {'A': 100, 'B': 200, 'C': 300, 'D': 400}
List of key value pair of dict_class_wise_count_copy -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500, 'F': 600}
In the above example, we have used the built-in copy() method and have created the copied dictionary. Then we have removed the last key “E” from original dictionary and added a new key in the copied dictionary. Both do not have the impact of one on another. So, this is an example of Deep copy.
RELATED TOPICS:
- INTRODUCTION TO PYTHON DICTIONARIES
- PYTHON NUMBER KEY VALUE IN DICTIONARY
- PYTHON STRING KEY VALUE IN DICTIONARY
- PYTHON MIXED KEY VALUE IN DICTIONARY
- PYTHON NESTED DICTIONARIES
- ADDING NEW ITEMS IN PYTHON DICTIONARY
- UPDATING PYTHON DICTIONARIES
- REMOVING ITEMS FROM PYTHON DICTIONARIES
- PYTHON DICTIONARY SORTING OPERATIONS
- PYTHON DICTIONARY BUILT IN FUNCTIONS
- PYTHON DICTIONARY SHALLOW COPY OPERATION