Python Practice › Tuple Programs › Program 19

Remove an item from a tuple

EasyTuple Programs · 19 of 70

Write a Python program to remove an item from a tuple.

Advertisement
Input
("p", "y", "t", "h", "o", "n"), remove "h"
Output
('p', 'y', 't', 'o', 'n')

Approach

  1. Build a new tuple without the item (tuples cannot be changed).

Solution

Python
t = ("p", "y", "t", "h", "o", "n")i = t.index("h")print(t[:i] + t[i + 1:])
Output
('p', 'y', 't', 'o', 'n')

Line by line

Line 2

Find the position, then join the slices around it. This removes only the first “h”.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs