Open In App

Python program to remove last element from set

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

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 4
Output: {1,2,3}

Input: {“Geeks”,”For”}
Remove Geeks
Output: {“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 element in the set has no fixed order and it can change therefore we cannot delete any particular element with respect to its position.

Remove the last element from the set Using discard() method

discard() method can also remove the element from the set, by passing the element into this function.

Python3




s = {1, 2, 3, 4}
s.discard(4)
print(s)
 
s = {"Geeks", "For"}
s.discard("Geeks")
print(s)


Output

{1, 2, 3}
{'For'}

Remove the last element from set  Using remove()

remove() can be also used in the same ways as we see in method 1.

Python3




s = {1, 2, 3, 4}
s.remove(4)
print(s)
 
s = {"Geeks", "For"}
s.remove("Geeks")
print(s)


Output

{1, 2, 3}
{'For'}

Note: If the element is not present in the set then the remove method raises an error so if you’re not sure that the element is present or not then use the discard method.

Remove the last element from the set Using pop() 

Python3




s = {1, 2, 3, 4}
s.pop()
print(s)
 
s = {"Geeks", "For"}
s.pop()
print(s)


Output

{2, 3, 4}
{'For'}

Note: pop() method deletes random elements in the set and it raises a error if the set is empty.

Remove the last element from the set using list

In this method we first convert a set into a list then we remove the last element in that list using slicing. And in last we convert that list back to set.

Python3




s = {1, 2, 3, 4}
s = set(list(s)[:-1])
print(s)
 
s = {"Geeks", "For"}
s = set(list(s)[:-1])
print(s)


Output

{1, 2, 3}
{'Geeks'}


Previous Article
Next Article

Similar Reads

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
3 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 | Remove last element from each row in Matrix
Sometimes, while working with Matrix data, we can have a stray element attached at rear end of each row of matrix. This can be undesired at times and wished to be removed. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + del + list slicing The combination of the above functionalities can be used to perform thi
6 min read
Remove Last Element from List in Python
Given a list, the task is to write a Python program to remove the last element present in the list and update the original list in Python. Example: Input: ["geeks", "for", "geeks"]Output:["geeks", "for"] Input: [1, 2, 3, 4, 5]Output:[1, 2, 3, 4] Explanation: Here simply we have to remove the last element present in the list and print the resultant
4 min read
Python - Remove last element from dictionary
Given a dictionary, the task is to remove the last occurring element, i.e. the last key-value pair in the dictionary. Example: Input: {1: 'Geeks', 2: 'For', 3: 'Geeks'} Output:{1: 'Geeks', 2: 'For'}Method 1: Using popitem() method Python dictionary popitem() method removes one key, value pair as a two-item tuple and modifies the removes the last ke
5 min read
Python program to remove last N characters from a string
Given a string S and an integer N, the task is to remove N characters from the end of the string S. Input: S = "GeeksForGeeks", N = 5Output: GeeksForExplanation: Removing the last 5 characters from "GeeksForGeeks" modifies the string to "GeeksFor". Input: S = "Welcome", N = 3Output: Welc Approach 1: Follow the steps below to solve the problem: Init
3 min read
Python program to Remove Last character from the string
Given a string, the task is to write a Python program to remove the last character from the given string. Example: Input: "GeeksForGeeks"Output: "GeeksForGeek" Input: "1234"Output: "123"Explanation: Here we are removing the last character of the original string. Note: Strings are immutable in Python, so any modification in the string will result in
3 min read
Python program to Remove the last item from array
Given an array, the task is to write a Python program to remove the last element in Python. Example: Input: ["geeks", "for", "geeks"] Output: ["geeks", "for"] Input: [1, 2, 3, 4, 5] Output: [1, 2, 3, 4] Explanation: Here simply we have to remove the last element present in the array and return the remaining array. Note: Arrays are not supported in
4 min read
Python | Slicing list from Kth element to last element
Python list slicing slices the list from start index till end - 1, specified as list elements. So its tricky when we require to also slice the last element of list. Trying to slice till list size + 1 gives an error. Let's discuss ways in which last element can be included during a list slice. Method #1 : Using None During list slicing, giving the d
6 min read
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
Article Tags :
Practice Tags :