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.