Remove the last element from Tuple Using negative indexing

Python3




# create a tuple
tuple = ("geeks", "for", "geeks")
 
# remove last element
tuple = tuple[:-1]
print(tuple)


Output

('geeks', 'for')

Explanation:

Here instead of finding the index of the last element we directly write -1( that denotes the last element in the iterable) and then simply slicing it till the last element and update the existing tuple.

Python program to remove last element from Tuple

Given a tuple, the task is to write a Python program to delete the last element in the tuple in Python.

Example:

Input: (“geeks”, “for”, “geeks”)

Output:(“geeks”, “for”)

Explanation: Here we are deleting the last element of the tuple and finally modifying the original one.

 Note: Tuples are immutable datatypes i.e. they cannot be modified therefore other than slicing we cannot modify a tuple but what we can do is that we can convert a tuple into list and then perform operations according to our need.

Similar Reads

Remove the last element from Tuple Using positive indexing

Python3 # create a tuple tuple = ("geeks", "for", "geeks")   # remove last element l_element = len(tuple)-1 tuple = tuple[:l_element] print(tuple)...

Remove the last element from Tuple Using negative indexing

...

Remove last element from Tuple Using lists

Python3 # create a tuple tuple = ("geeks", "for", "geeks")   # remove last element tuple = tuple[:-1] print(tuple)...

Remove last element from Tuple Using lists remove() method

...

Remove last element from Tuple Using the filter() function.

Python3 # create a list tu = (1, 2, 3, 4, 5)   # remove last element tu = list(tu) tu.pop(-1) tu = tuple(tu) print(tu)...