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)


Output

('geeks', 'for')

Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of tuple.

Explanation:

Here simply we find the index of the last element as (length of tuple-1) and then we update the current tuple with the modified or slicing tuple till the last element(excluding it).

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)...