Sayantan's Blog On Python Programming

IMMUTABILITY OF TUPLE IN PYTHON

IMMUTABILITY OF TUPLE IN PYTHON

INTRODUCTION

Tuple in python are immutable in nature. It means that once a tuple is created can not be changed or updated.

Updating a Tuple

Updating a primitive type inside a Tuple

We can not update a tuple. If we try then python will raise error.

# Updating a Tuple example 1 

int_tuple = (400, 200, 600, 100, 150, 600, 500) 
print("\nFields of tuple int_tuple ->", int_tuple) 

int_tuple[0] = 40
print("\nFields of tuple int_tuple ->", int_tuple) 

IMMUTABILITY OF TUPLE IN PYTHON : Output

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

Updating a Tuple inside a Tuple

If we try to update a nested tuple then also python will raise the error.

# Updating a Tuple example 2

int_tuple = (400, 200, 600, 100, 150, (600, 500)) 
print("\nFields of tuple int_tuple ->", int_tuple) 

print("\nFields of tuple int_tuple at position 6 ->", int_tuple[5]) 

int_tuple[5] = (700, 200)

print("\nFields of tuple int_tuple at position 6 ->", int_tuple[5])

IMMUTABILITY OF TUPLE IN PYTHON : Output

Fields of tuple int_tuple -> (400, 200, 600, 100, 150, (600, 500))

Fields of tuple int_tuple at position 6 -> (600, 500)
Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\Untitled-1.py", line 21, in <module>
    int_tuple[5] = (700, 200)
    ~~~~~~~~~^^^
TypeError: 'tuple' object does not support item assignment

In the above example we can see that the last field of the tuple is a tuple itself. If we try to update the last field with another tuple then python has raised error.

# Updating a Tuple example 3

int_tuple = (400, 200, 600, 100, 150, (600, 500)) 
print("\nFields of tuple int_tuple ->", int_tuple) 

print("\nFields of tuple int_tuple at position 6 ->", int_tuple[5]) 

int_tuple[5][0] = 700

print("\nFields of tuple int_tuple at position 6 ->", int_tuple[5])

IMMUTABILITY OF TUPLE IN PYTHON : Output

Fields of tuple int_tuple -> (400, 200, 600, 100, 150, (600, 500))

Fields of tuple int_tuple at position 6 -> (600, 500)
Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\Untitled-1.py", line 21, in <module>
    int_tuple[5][0] = 700
    ~~~~~~~~~~~~^^^
TypeError: 'tuple' object does not support item assignment

In the above example we can see that when we try to update the first field of the inner tuple, then also python has raised the error.

Updating a List inside a Tuple

If we try to update a list item as a field in a tuple, then python will raise error.

# Updating a Tuple example 4

int_tuple = (400, 200, 600, 100, 150, [600, 500]) 
print("\nFields of tuple int_tuple ->", int_tuple) 

print("\nFields of tuple int_tuple at position 6 ->", int_tuple[5]) 

int_tuple[5] = [700, 200]

print("\nFields of tuple int_tuple at position 6 ->", int_tuple[5])

IMMUTABILITY OF TUPLE IN PYTHON : Output

Fields of tuple int_tuple -> (400, 200, 600, 100, 150, [600, 500])

Fields of tuple int_tuple at position 6 -> [600, 500]
Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.py", line 8, in <module>
    int_tuple[5] = [700, 200]
    ~~~~~~~~~^^^
TypeError: 'tuple' object does not support item assignment

But if we try to update an individual element of a list contained in a tuple then python allows that.

# Updating a Tuple example 5

int_tuple = (400, 200, 600, 100, 150, [600, 500]) 
print("\nFields of tuple int_tuple ->", int_tuple) 

print("\nFields of tuple int_tuple at position 6 ->", int_tuple[5]) 

int_tuple[5][0] = 700
int_tuple[5][1] = 200

print("\nFields of tuple int_tuple at position 6 ->", int_tuple[5])

IMMUTABILITY OF TUPLE IN PYTHON : Output

Fields of tuple int_tuple -> (400, 200, 600, 100, 150, [600, 500])

Fields of tuple int_tuple at position 6 -> [600, 500]

Fields of tuple int_tuple at position 6 -> [700, 200]

Deleting a Tuple

Python does not allow to delete an individual field of a tuple. But we can delete a tuple as whole by using del command.

Deleting an individual field

# Deleting a Tuple example 1

int_tuple = (400, 200, 600, 100, 150, [600, 500]) 
print("\nFields of tuple int_tuple ->", int_tuple) 

del int_tuple[0]

print("\nFields of tuple int_tuple ->", int_tuple) 

IMMUTABILITY OF TUPLE IN PYTHON : Output

Fields of tuple int_tuple -> (400, 200, 600, 100, 150, [600, 500])
Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\Untitled-1.py", line 19, in <module>
    del int_tuple[0]
        ~~~~~~~~~^^^
TypeError: 'tuple' object doesn't support item deletion

# Deleting a Tuple example 2

str_tuple = ('Python', 'Java', 'C++', 'C', ['PHP', 'XML']) 
print("\nFields of tuple str_tuple ->", str_tuple) 

del str_tuple[0]

print("\nFields of tuple str_tuple ->", str_tuple) 

IMMUTABILITY OF TUPLE IN PYTHON : Output

Fields of tuple str_tuple -> ('Python', 'Java', 'C++', 'C', ['PHP', 'XML'])
Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.py", line 6, in <module>
    del str_tuple[0]
        ~~~~~~~~~^^^
TypeError: 'tuple' object doesn't support item deletion

Deleting a tuple as whole

# Deleting a Tuple example 3

int_tuple = (400, 200, 600, 100, 150, [600, 500]) 
print("\nFields of tuple int_tuple ->", int_tuple) 

del int_tuple
print("\nint_tuple has been deleted..!!\n\n") 

print("\nNow Fields of tuple int_tuple ->", int_tuple)

IMMUTABILITY OF TUPLE IN PYTHON : Output

Fields of tuple int_tuple -> (400, 200, 600, 100, 150, [600, 500])

int_tuple has been deleted..!!


Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\Untitled-1.py", line 22, in <module>
    print("\nNow Fields of tuple int_tuple ->", int_tuple) 
                                                ^^^^^^^^^
NameError: name 'int_tuple' is not defined

# Deleting a Tuple example 4

str_tuple = ('Python', 'Java', 'C++', 'C', ['PHP', 'XML']) 
print("\nFields of tuple str_tuple ->", str_tuple) 

del str_tuple
print("\nstr_tuple has been deleted..!!\n\n") 

print("\nNow Fields of tuple str_tuple ->", str_tuple) 

IMMUTABILITY OF TUPLE IN PYTHON : Output

Fields of tuple str_tuple -> ('Python', 'Java', 'C++', 'C', ['PHP', 'XML'])

str_tuple has been deleted..!!


Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.py", line 9, in <module>
    print("\nNow Fields of tuple str_tuple ->", str_tuple) 
                                                ^^^^^^^^^
NameError: name 'str_tuple' is not defined

So in both the example above, we can see that the outer tuple has been deleted and freed up the memory.

IMMUTABILITY OF TUPLE IN PYTHON : Output
IMMUTABILITY OF TUPLE IN PYTHON : Output

RELATED TOPICS:

Leave a Comment

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