Sometimes, while working with Python tuples, we can have a problem in which we need to perform the removal of first occurrence of an element in tuple. This type of problem can have application in many domains such as school programming. Let’s discuss certain ways in which this task can be performed.

Input: test_tuple = (5, 6, 5, 4, 7, 8, 4), K = 5

Output: (6, 5, 4, 7, 8, 4)

Input: test_tuple = (5, 6, 8, 4, 7, 8, 4), K = 8

Output: (5, 6, 4, 7, 8, 4)

**Method #1 : Using index() + loop + list slicing**

The combination of above functions can be used to solve this problem. In this, we perform the task of extracting the first occurrence of K using index() and list slicing is used to reorder the tuple after element removal.

`# Python3 code to demonstrate working of ` `# Remove first occurrence of K in Tuple ` `# Using index() + loop + list slicing ` ` ` `# initializing tuples ` `test_tuple ` `=` `(` `5` `, ` `6` `, ` `4` `, ` `4` `, ` `7` `, ` `8` `, ` `4` `) ` ` ` `# printing original tuple ` `print` `(` `"The original tuple : "` `+` `str` `(test_tuple)) ` ` ` `# initializing K ` `K ` `=` `4` ` ` `# Remove first occurrence of K in Tuple ` `# Using index() + loop + list slicing ` `try` `: ` ` ` `idx ` `=` `test_tuple.index(K) ` ` ` `res ` `=` `test_tuple[:idx] ` `+` `test_tuple[idx ` `+` `1` `:] ` `except` `ValueError: ` ` ` `res ` `=` `test_tuple ` ` ` `# printing result ` `print` `(` `"Tuple after element removal : "` `+` `str` `(res)) ` |

*chevron_right*

*filter_none*

**Output :**

The original tuple : (5, 6, 4, 4, 7, 8, 4) Tuple after element removal : (5, 6, 4, 7, 8, 4)

**Method #2 : Using enumerate() + generator expression**

This is one of the ways in which this task can be performed. This offers one liner way to solve this problem. In this, we perform the task of checking for element and index using enumerate().

`# Python3 code to demonstrate working of ` `# Remove first occurrence of K in Tuple ` `# Using enumerate() + generator expression ` ` ` `# initializing tuples ` `test_tuple ` `=` `(` `5` `, ` `6` `, ` `4` `, ` `4` `, ` `7` `, ` `8` `, ` `4` `) ` ` ` `# printing original tuple ` `print` `(` `"The original tuple : "` `+` `str` `(test_tuple)) ` ` ` `# initializing K ` `K ` `=` `4` ` ` `# Remove first occurrence of K in Tuple ` `# Using enumerate() + generator expression ` `res ` `=` `tuple` `(ele ` `for` `idx, ele ` `in` `enumerate` `(test_tuple) ` `if` `idx !` `=` `test_tuple.index(K)) ` ` ` `# printing result ` `print` `(` `"Tuple after element removal : "` `+` `str` `(res)) ` |

*chevron_right*

*filter_none*

**Output :**

The original tuple : (5, 6, 4, 4, 7, 8, 4) Tuple after element removal : (5, 6, 4, 7, 8, 4)

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course.

## Recommended Posts:

- Python | Sort tuple based on occurrence of first element
- Python | Count occurrence of all elements of list in a tuple
- Python - Remove given character from first element of Tuple
- Python | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python program to convert Set into Tuple and Tuple into Set
- Python program to remove each y occurrence before x in List
- Python program to remove Nth occurrence of the given word
- Python | First occurrence of True number
- Python | First character occurrence from rear String
- Python - Substitute K for first occurrence of elements
- Python | Remove tuple from list of tuples if not containing any character
- Python | Remove particular element from tuple list
- Python | Remove Consecutive tuple according to key
- Python | Remove similar element rows in tuple Matrix

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.