Open In App

Python program to remove last element from Tuple

Last Updated : 27 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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

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.

Remove last element from Tuple Using lists

Python3




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


Output

(1, 2, 3, 4)

Explanation:

As tuples are immutable we cannot directly modify them therefore first we convert it into lists then pop the last element.

Remove last element from Tuple Using lists remove() method 

Python3




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


Output

(1, 2, 3, 4)

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

Algorithm:

  1. Define a tuple “tu” containing the values (1, 2, 3, 4, 5).
  2. Use the filter function with a lambda function to remove the last element of the tuple. The lambda function checks
  3. whether the current element is not equal to the last element of the tuple.
  4. Convert the resulting filter object back to a tuple and store it in the variable “tu”.
  5. Print the updated tuple “tu”.

Python3




tu = (1, 2, 3, 4, 5)
tu = tuple(filter(lambda x: x != tu[-1], tu))
print(tu)


Output

(1, 2, 3, 4)

Time complexity:

The time complexity of this algorithm is O(n), where n is the length of the tuple “tu”. This is because the filter function and the tuple conversion both have a linear time complexity in the size of the input, and the lambda function is executed n times for a tuple of length n.

Auxiliary Space:

The space complexity of this algorithm is O(n), where n is the length of the tuple “tu”. This is because a new tuple is created to store the filtered elements, which requires memory proportional to the size of the input tuple.



Previous Article
Next Article

Similar Reads

Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
The task is to write a Python Program to sort a list of tuples in increasing order by the last element in each tuple. Input: [(1, 3), (3, 2), (2, 1)] Output: [(2, 1), (3, 2), (1, 3)] Explanation: sort tuple based on the last digit of each tuple. Methods #1: Using sorted(). Sorted() method sorts a list and always returns a list with the elements in
7 min read
Python | Sort tuple list by Nth element of tuple
Sometimes, while working with Python list, we can come across a problem in which we need to sort the list according to any tuple element. These must be a generic way to perform the sort by particular tuple index. This has a good utility in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using sort(
8 min read
Python | Replace tuple according to Nth tuple element
Sometimes, while working with data, we might have a problem in which we need to replace the entry in which a particular entry of data is matching. This can be a matching phone no, id etc. This has it's application in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + enumerate() This task
8 min read
Python Program to Convert Tuple Matrix to Tuple List
Given a Tuple Matrix, flatten to tuple list with each tuple representing each column. Example: Input : test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)]] Output : [(4, 7, 10, 18), (5, 8, 13, 17)] Explanation : All column number elements contained together. Input : test_list = [[(4, 5)], [(10, 13)]] Output : [(4, 10), (5, 13)] Explanation : All co
8 min read
Python program to convert Set into Tuple and Tuple into Set
Let's see how to convert the set into tuple and tuple into the set. For performing the task we are use some methods like tuple(), set(), type(). tuple(): tuple method is used to convert into a tuple. This method accepts other type values as an argument and returns a tuple type value.set(): set method is to convert other type values to set this meth
7 min read
Python Program to find tuple indices from other tuple list
Given Tuples list and search list consisting of tuples to search, our task is to write a Python Program to extract indices of matching tuples. Input : test_list = [(4, 5), (7, 6), (1, 0), (3, 4)], search_tup = [(3, 4), (8, 9), (7, 6), (1, 2)]Output : [3, 1]Explanation : (3, 4) from search list is found on 3rd index on test_list, hence included in r
8 min read
Python Program to Merge tuple list by overlapping mid tuple
Given two lists that contain tuples as elements, the task is to write a Python program to accommodate tuples from the second list between consecutive tuples from the first list, after considering ranges present between both the consecutive tuples from the first list. Input : test_list1 = [(4, 8), (19, 22), (28, 30), (31, 50)], test_list2 = [(10, 12
11 min read
Python program to Remove the last element from list
Given a list of numbers or strings, the task is to write a Python program to remove the last element from a list. Example: Input : [12,53,11,76,22,21] Output : [12,53,11,76,22] Input : ["abc","gfg","text","python"] Output : ["abc","gfg","text"] The application of this type of problem in day-day programming when sometimes requires getting all the el
3 min read
Python program to remove last element from set
Given a set, the task is to write a Python program to delete the last element from the set. Example: Input: {1,2,3,4}Remove 4Output: {1,2,3} Input: {"Geeks","For"}Remove GeeksOutput: {"For"}Explanation: The idea is to remove the last element from the set 4 in the first case and "Geeks" in the second case. Note: Sets are unordered which means any el
2 min read
Python | Remove similar element rows in tuple Matrix
Sometimes, while working with data, we can have a problem in which we need to remove elements from the tuple matrix on a condition that if all elements in row of tuple matrix is same. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + all() This task can be performed using combination of above fun
6 min read
Article Tags :
Practice Tags :