Sayantan's Blog On Python Programming

PYTHON TUPLE SHALLOW COPY

PYTHON TUPLE SHALLOW COPY

INTRODUCTION

There are two types of copy operations are available in Python.

  • SHALLOW COPY
  • DEEP COPY

In this article we will discuss on SHALLOW copy operations. Shallow copy operation represents that if we change or modify any of the tuple then the other tuple will also be impacted.

There are several ways we can create a shallow copy of a tuple in python.

  • Assignment Operation
  • Importing Copy Module

Assignment Operation

# Copy operation using assignment operator example 1

org_tuple = (400, 200, 600, 100, 150, 500)
print("\nField of original tuple ->", org_tuple)

copy_tuple = org_tuple
print("\nField of copied tuple ->", copy_tuple)

PYTHON TUPLE SHALLOW COPY : Output

Field of original tuple -> (400, 200, 600, 100, 150, 500)

Field of copied tuple -> (400, 200, 600, 100, 150, 500)

In the above example we can see that with the assignment operator we have created a copied tuple and both the original and copied tuple have identical fields.

Tuples are immutable in nature. Once they are created cannot be changed any way.

# Copy operation using assignment operator example 2

org_tuple = (400, 200, 600, 100, 150, 500)
print("\nField of original tuple ->", org_tuple)

copy_tuple = org_tuple
print("\nField of copied tuple ->", copy_tuple)

# Removing the last field from the copied tuple 
copy_tuple.pop()

print("\nField of original tuple ->", org_tuple)
print("\nField of copied tuple ->", copy_tuple)

PYTHON TUPLE SHALLOW COPY : Output

Field of original tuple -> (400, 200, 600, 100, 150, 500)

Field of copied tuple -> (400, 200, 600, 100, 150, 500)
Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\Untitled-1.py", line 15, in <module>
    copy_tuple.pop()
    ^^^^^^^^^^^^^^
AttributeError: 'tuple' object has no attribute 'pop'

In the above example, we tried to remove one field from the end and python has raised the error.

# Copy operation using assignment operator example 3

org_tuple = (400, 200, 600, 100, 150, 500)
print("\nField of original tuple ->", org_tuple)

copy_tuple = org_tuple
print("\nField of copied tuple ->", copy_tuple)

# Updating the first tuple of the copied tuple 
copy_tuple[0] = 700

print("\nField of original tuple ->", org_tuple)
print("\nField of copied tuple ->", copy_tuple)

PYTHON TUPLE SHALLOW COPY : Output

Field of original tuple -> (400, 200, 600, 100, 150, 500)

Field of copied tuple -> (400, 200, 600, 100, 150, 500)
Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.py", line 10, in <module>
    copy_tuple[0] = 700
    ~~~~~~~~~~^^^
TypeError: 'tuple' object does not support item assignment

In the above example, we have tried to assign a new value in the first field, so python has raised the error.

But tuple may contain other complex data types like list. List are mutable in nature, so we can make the changes.

# Copy operation using assignment operator example 4

org_tuple = (400, 200, 600, [100, 150, 500])
print("\nField of original tuple ->", org_tuple)

copy_tuple = org_tuple
print("\nField of copied tuple ->", copy_tuple)

# Updating the first element of the list in the original tuple 
org_tuple[3][0] = 700

print("\nField of original tuple ->", org_tuple)
print("\nField of copied tuple ->", copy_tuple)

PYTHON TUPLE SHALLOW COPY : Output

Field of original tuple -> (400, 200, 600, [100, 150, 500])

Field of copied tuple -> (400, 200, 600, [100, 150, 500])

Field of original tuple -> (400, 200, 600, [700, 150, 500])

Field of copied tuple -> (400, 200, 600, [700, 150, 500])

In the above example, we have included one list inside the tuple. We have updated the first element of the list in the original tuple. The change is also visible in the copied tuple. So its a example of shallow copy. After including list item inside the tuple python allows the changes in tuple.

# Copy operation using assignment operator example 5

int_list = [100, 150, 500]
org_tuple = (400, 200, 600, int_list)
print("\nField of original tuple ->", org_tuple)

copy_tuple = org_tuple
print("\nField of copied tuple ->", copy_tuple)

# Updating the first element of the list in the original tuple 
int_list[0] = 700

print("\nField of original tuple ->", org_tuple)
print("\nField of copied tuple ->", copy_tuple)

int_list.pop()

print("\nField of original tuple ->", org_tuple)
print("\nField of copied tuple ->", copy_tuple)

PYTHON TUPLE SHALLOW COPY : Output

Field of original tuple -> (400, 200, 600, [100, 150, 500])

Field of copied tuple -> (400, 200, 600, [100, 150, 500])

Field of original tuple -> (400, 200, 600, [700, 150, 500])

Field of copied tuple -> (400, 200, 600, [700, 150, 500])

Field of original tuple -> (400, 200, 600, [700, 150])

Field of copied tuple -> (400, 200, 600, [700, 150])

In the above example, we have used a list variable as a field inside the tuple. So any change in the list variable will change the tuple.

Importing Copy Module

Python provides several built-in modules which can be utilized in our programs to make it more efficient and productive. To use the built-in modules we will use the import command in our program. By reusing the codes of built-in modules our programs will look compact and easy to understand.

# Copy operation using copy() example 1

import copy

org_tuple = (400, 200, 600, 100, 150, 500)
print("\nField of original tuple ->", org_tuple)

copy_tuple = copy.copy(org_tuple)
print("\nField of copied tuple ->", copy_tuple)

PYTHON TUPLE SHALLOW COPY : Output

Field of original tuple -> (400, 200, 600, 100, 150, 500)

Field of copied tuple -> (400, 200, 600, 100, 150, 500)

In the above example, we can see that with the copy() method of copy module we have created a copied tuple and both the original and copied tuple have identical fields.

Now since Tuples are immutable in nature, if we try to change the tuple then python will raise the error as we saw in case of assignment operator. So, we will include one list item inside the tuple. List are mutable in nature, hence we can make the changes.

# Copy operation using copy() example 2

import copy

int_list = [100, 150, 500]
org_tuple = (400, 200, 600, int_list)
print("\nField of original tuple ->", org_tuple)

copy_tuple = copy.copy(org_tuple)
print("\nField of copied tuple ->", copy_tuple)

# Updating the first element of the list in the original tuple 
int_list[0] = 700

print("\nField of original tuple ->", org_tuple)
print("\nField of copied tuple ->", copy_tuple)

int_list.pop()

print("\nField of original tuple ->", org_tuple)
print("\nField of copied tuple ->", copy_tuple)

PYTHON TUPLE SHALLOW COPY : Output

Field of original tuple -> (400, 200, 600, [100, 150, 500])

Field of copied tuple -> (400, 200, 600, [100, 150, 500])

Field of original tuple -> (400, 200, 600, [700, 150, 500])

Field of copied tuple -> (400, 200, 600, [700, 150, 500])

Field of original tuple -> (400, 200, 600, [700, 150])

Field of copied tuple -> (400, 200, 600, [700, 150])

In the above example, we have updated the first element of the list inside the tuple and python has allowed that. Also we have removed the last element of the list.

# Copy operation using copy() example 3

import copy

int_list = [100, 150, 500]
org_tuple = (400, 200, 600, int_list)
print("\nField of original tuple ->", org_tuple)

copy_tuple = copy.copy(org_tuple)
print("\nField of copied tuple ->", copy_tuple)

# Updating the first element of the list in the original tuple 
int_list[0] = 700

print("\nField of original tuple ->", org_tuple)
print("\nField of copied tuple ->", copy_tuple)

int_list[2] = 300
int_list.append(900)

print("\nField of original tuple ->", org_tuple)
print("\nField of copied tuple ->", copy_tuple)

PYTHON TUPLE SHALLOW COPY : Output

Field of original tuple -> (400, 200, 600, [100, 150, 500])

Field of copied tuple -> (400, 200, 600, [100, 150, 500])

Field of original tuple -> (400, 200, 600, [700, 150, 500])

Field of copied tuple -> (400, 200, 600, [700, 150, 500])

Field of original tuple -> (400, 200, 600, [700, 150, 300, 900])

Field of copied tuple -> (400, 200, 600, [700, 150, 300, 900])

In the above example, we have updated the first and third elements of the list and added the new element 900 at index position 3. All are allowed.

PYTHON TUPLE SHALLOW COPY : Output
PYTHON TUPLE SHALLOW COPY : Output

RELATED TOPICS:

Leave a Comment

Your email address will not be published. Required fields are marked *