Remove Tuple Item

Tags: tuple

Given a tuple, remove an item from it. As tuples are immutable, you will need to convert the tuple to a list, remove the item, and then convert it back to a tuple. Write a Python program to implement this.

Example 1:

Input: (1, 2, 3, 4, 5), 3 
Output: (1, 2, 4, 5)

Example 2:

Input: ('a', 'b', 'c', 'd', 'e'), 'c' 
Output: ('a', 'b', 'd', 'e')

You need to convert the tuple to a list first, then perform the removal operation and convert the list back to a tuple.

def remove_item(t, item):
    l = list(t)
    return tuple(l)

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!