PYTHON TUPLE DEEP COPY
INTRODUCTION
In deep copy operation, changes in original tuple does not have the impact on copied tuple and vice-versa.
There is only one way we can create deep copy in Python for tuple.
- Importing copy Module to use deepcopy() method
Importing copy Module to use deepcopy() method
Example 1
# Deep Copy operation using Slicing operator[:] example 1
import copy
int_list = [1, 2, 5]
org_tuple = [400, 200, 600, 100, 150, 500, int_list]
print("\nField of original tuple ->", org_tuple)
copy_tuple = copy.deepcopy(org_tuple)
print("\nField of copied tuple ->", org_tuple)
PYTHON TUPLE DEEP COPY : Output
Field of original tuple -> [400, 200, 600, 100, 150, 500, [1, 2, 5]]
Field of copied tuple -> [400, 200, 600, 100, 150, 500, [1, 2, 5]]
Example 2
# Deep Copy operation using Slicing operator[:] example 2
import copy
int_list = [1, 2, 5]
org_tuple = [400, 200, 600, 100, 150, 500, int_list]
print("\nField of original tuple ->", org_tuple)
copy_tuple = copy.deepcopy(org_tuple)
print("\nField of copied tuple ->", org_tuple)
# Removing the last element from the list item from original tuple
int_list.remove(5)
print("\nField of original tuple ->", org_tuple)
print("\nField of copied tuple ->", copy_tuple)
PYTHON TUPLE DEEP COPY : Output
Field of original tuple -> [400, 200, 600, 100, 150, 500, [1, 2, 5]]
Field of copied tuple -> [400, 200, 600, 100, 150, 500, [1, 2, 5]]
Field of original tuple -> [400, 200, 600, 100, 150, 500, [1, 2]]
Field of copied tuple -> [400, 200, 600, 100, 150, 500, [1, 2, 5]]
In the above example, we can see that we have removed the last element of the list item which was a field of original tuple. But this change doesn’t affect the copied tuple. So its an example of deep copy.
Example 3
# Deep Copy operation using Slicing operator[:] example 3
import copy
int_list = [1, 2, 5]
str_list = ['Python', 'Java', 'C++']
org_tuple = [400, 200, 600, 100, 150, 500, int_list, str_list]
print("\nField of original tuple ->", org_tuple)
copy_tuple = copy.deepcopy(org_tuple)
print("\nField of copied tuple ->", org_tuple)
# Updating the second element from the string list item from original tuple
str_list[1] = 'Word'
print("\nField of original tuple ->", org_tuple)
print("\nField of copied tuple ->", copy_tuple)
PYTHON TUPLE DEEP COPY : Output
Field of original tuple -> [400, 200, 600, 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'C++']]
Field of copied tuple -> [400, 200, 600, 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'C++']]
Field of original tuple -> [400, 200, 600, 100, 150, 500, [1, 2, 5], ['Python', 'Word', 'C++']]
Field of copied tuple -> [400, 200, 600, 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'C++']]
In the above example, we have used a string list item in the original tuple. Then we have updated the string list element at index position 1 to ‘Word’. This change is visible in original tuple but it did not affect the copied tuple. So deep copy has been done in this case as well.
Example 4
# Deep Copy operation using Slicing operator[:] example 4
import copy
int_list = [1, 2, 5]
str_list = ['Python', 'Java', 'C++']
org_tuple = [400, 200, 600, 100, 150, 500, int_list, str_list]
print("\nField of original tuple ->", org_tuple)
copy_tuple = copy.deepcopy(org_tuple)
print("\nField of copied tuple ->", org_tuple)
# Adding new elements to both integer and string list item of original tuple
str_list.append('Word')
int_list.append(8)
print("\nField of original tuple ->", org_tuple)
print("\nField of copied tuple ->", copy_tuple)
PYTHON TUPLE DEEP COPY : Output
Field of original tuple -> [400, 200, 600, 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'C++']]
Field of copied tuple -> [400, 200, 600, 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'C++']]
Field of original tuple -> [400, 200, 600, 100, 150, 500, [1, 2, 5, 8], ['Python', 'Java', 'C++', 'Word']]
Field of copied tuple -> [400, 200, 600, 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'C++']]
In the above example we have added a new element to both integer list and string list. This is visible in original tuple but the copied tuple is not impacted at all. So deep copy rule is intact.
Example 5
# Deep Copy operation using Slicing operator[:] example 5
import copy
int_list = [1, 2, 5]
str_list = ['Python', 'Java', 'C++']
org_tuple = [400, 200, 600, 100, 150, 500, int_list, str_list]
print("\nField of original tuple ->", org_tuple)
copy_tuple = copy.deepcopy(org_tuple)
print("\nField of copied tuple ->", org_tuple)
# Adding new elements to both integer and string list item of original tuple
str_list.insert(1, 'Word')
int_list.pop()
print("\nField of original tuple ->", org_tuple)
print("\nField of copied tuple ->", copy_tuple)
PYTHON TUPLE DEEP COPY : Output
Field of original tuple -> [400, 200, 600, 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'C++']]
Field of copied tuple -> [400, 200, 600, 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'C++']]
Field of original tuple -> [400, 200, 600, 100, 150, 500, [1, 2], ['Python', 'Word', 'Java', 'C++']]
Field of copied tuple -> [400, 200, 600, 100, 150, 500, [1, 2, 5], ['Python', 'Java', 'C++']]
In the above example, we we have inserted a new element at index position 1 in string list and have removed the last element from the integer list. These changes are only visible in original tuple and not in copied tuple.
RELATED TOPICS:
- INTRODUCTION TO PYTHON TUPLE
- ACCESSING NUMBER TUPLE IN PYTHON
- ACCESSING STRING TUPLE IN PYTHON
- ACCESSING MIXED TUPLE IN PYTHON
- ACCESSING NESTED TUPLE IN PYTHON
- PYTHON TOUPLE SLICING OPERATION
- STEP SIZE IN PYTHON TUPLE SLICING
- IMMUTABILITY OF TUPLE IN PYTHON
- ZIP FUNCTION IN PYTHON TUPLE
- PYTHON TUPLE SORTING OPERATIONS
- PYTHON TUPLE BUILT-IN FUNCTIONS
- REOVING TUPLE DUPLICATES USING SET
- PYTHON TUPLE SHALLOW COPY