Remove Items from a Python Tuple



A tuple in Python is an ordered, immutable collection that holds multiple items, supports mixed data types, and allows indexing. Since tuples cannot be modified, index slicing can exclude specific elements when needed.

  • Conversion to a List

  • Applying Tuple Conversion

  • Slicing

Conversion to a List

Converting a tuple into a list is a common approach to modify its content since tuples are immutable. Using list(), we can remove or add elements, then reconvert it to a tuple using tuple() to preserve the final structure.

Example

This code converts the tuple t= (4, 2, 5, 6) into a list, removes the value 2 using remove(2), then converts it back into a tuple.

t = (4, 2, 5, 6)
temp = list(t)
temp.remove(2)       
t = tuple(temp)
print(t)

We will get the output as follows ?

(4, 5, 6)

Applying Tuple Conversion

Applying tuple conversion changes another data type, like a list or string, into a tuple using the tuple() function. This is useful when we need an immutable sequence.

Example

This code filters out the value 2 from the tuple t= (4, 2, 5, 6) using an expression. This crates a new tuple without 2 and print (4, 5, 6).

t = (4, 2, 5, 6)

t = tuple(x for x in t if x != 2)
print(t)

The result is obtained as follows ?

(4, 5, 6)

Example

Here we are converting a tuple a = (1, 2, 3, 4) into a list, deletes the third element(3) using del ls_a[2], then converts it back into a tuple.

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)

Tuple Modification using Slicing

Slicing in a tuple means extracting the elements using its elements using the syntax tuple[start:stop:step]. It returns a new tuple without modifying the original list.

Example

We are creating a tuple t=(20, 30, 40, 50), removes the second and third elements (30 and 40) using slicing, the prints the modified tuple.

t = (20, 30, 40, 50)

t = t[:1] + t[3:]
print(t)

The out is generated as follows ?

(20, 50)

Example

This code removes the third element(3) from tuple a using slicing, then stores the modified tuple in b. Printing b outputs as (1, 2, 3, 4, 5).

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

This will give the output:

(1, 2, 4, 5)
Updated on: 2025-04-17T11:06:57+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements