Python Practice › Tuple Programs › Program 19
Remove an item from a tuple
Write a Python program to remove an item from a tuple.
Input
("p", "y", "t", "h", "o", "n"), remove "h"Output
('p', 'y', 't', 'o', 'n')Approach
- Build a new tuple without the item (tuples cannot be changed).
Solution
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”.
Learn the concept
New to this topic? Read the lesson, then come back.