How can I remove items out of a Python tuple?


Tuples in python are immutable. If you want to remove items out of a Python tuple, you can use index slicing to leave out a particular index. For example,

a = (1, 2, 3, 4, 5)
b = a[:2] + a[3:]
print(b)

This will give the output:

(1, 2, 4, 5)

Or you can convert it to a list, remove the item and convert back to a tuple. For example,

a = (1, 2, 3, 4, 5)
ls_a = list(a)
del ls_a[2]

b = tuple(ls_a)
print(b)

This will give the output:

(1, 2, 4, 5)

Updated on: 17-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements