How can I remove items out of a Python tuple?

PythonProgramming

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)
raja
Published on 18-Apr-2018 14:09:25
Advertisements