How to remove the last element from a set in Python?


In this article, we will learn how to remove the last element from the set in Python.

Methods Used

The following are the various methods to accomplish this task −

  • Using the discard() function

  • Using the remove() function

  • Using the pop() function

  • Using the list slicing

Method 1: Using the discard() function

The discard() function removes the element from the set, bypassing the element as an argument to it.

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

  • Create a variable to store the input set containing integers.

  • Use the discard() function to remove the last element from the set by passing the last element of the set as an argument to it.

  • Print the resultant set after removing the last element from the input set.

  • In the same way, check it out for the set containing string elements.

Example

The following program removes the last element from the set using the discard() function −

# input set containing integers
inputSet = {5, 7, 2, 1, 8}
print("The Given Set is:", inputSet)
# removing the last element from the set by passing the
# last element as an argument to the discard() function
inputSet.discard(8)
# printing the resultant set after removing the last element
print("Given Set after Removing Last Element", inputSet)
# input set containing strings
inputSet_1 = {"Hello", "Tutorialspoint", "python"}
print("The Given Set is:", inputSet)
# similarly removing the last element "python" from the set
inputSet_1.discard("python")
# printing the resultant set
print("Given Set after Removing Last Element", inputSet_1)

Output

On execution, the above program will generate the following output −

The Given Set is: {1, 2, 5, 7, 8}
Given Set after Removing Last Element {1, 2, 5, 7}
The Given Set is: {1, 2, 5, 7}
Given Set after Removing Last Element {'Tutorialspoint', 'Hello'}

Method 2: Using the remove() function

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

  • Use the remove() function to remove the last element from the set by passing the last element of the set as an argument to it.

  • Print the resultant set after removing the last element from the input set.

Example

The following program removes the last element from the set using the remove() function −

# input set containing integers
inputSet = {5, 7, 2, 1, 8}
print("The Given Set is:", inputSet)
# removing the last element from the set by passing the
# last element as an argument to the remove() function
inputSet.remove(8)
# printing the resultant set after removing the last element
print("Given Set after Removing Last Element", inputSet)
# input set containing strings
inputSet_1 = {"Hello", "Tutorialspoint", "python"}
print("The Given Set is:", inputSet)
# similarly removing the last element "python" from the set
inputSet_1.remove("python")
# printing the resultant set
print("Given Set after Removing Last Element", inputSet_1)

Output

On execution, the above program will generate the following output −

The Given Set is: {1, 2, 5, 7, 8}
Given Set after Removing Last Element {1, 2, 5, 7}
The Given Set is: {1, 2, 5, 7}
Given Set after Removing Last Element {'Hello', 'Tutorialspoint'}

Method 3: Using the pop() function

The pop() method in python deletes random elements from the set. If the set is empty, an error is raised.

Use the pop() method, to remove a random element from the input set.

Example

The following program removes the random element from the input set using the pop() function −

# input set containing integers
inputSet = {5, 7, 2, 1, 8}
# removing random element from the set using pop()
inputSet.pop()
# printing the resultant set after removing any random element
print(inputSet)
# input set containing strings
inputSet_1 = {"Hello", "Tutorialspoint", "python"}
# similarly removing a random element from the set containing strings using pop()
inputSet_1.pop()
# printing the resultant set
print(inputSet_1)

Output

On execution, the above program will generate the following output −

{2, 5, 7, 8}
{'Hello', 'python'}

Method 4: Using the list slicing

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

  • Convert the input set to a list using the list() function (returns a list of an iterable) and remove the last element from the set using slicing i.e, taking all the elements except the last and then again converting the result to a set using the set() function.

  • set() function − creates a set object. A set list will appear in random order because the items are not ordered. It removes all the duplicates

  • Print the resultant set after removing the last element from the input set.

Example

The following program removes the last element from the set using the list slicing −

# input set containing integers
inputSet = {5, 7, 2, 1, 8}
print("The Given Set is:", inputSet)
# converting the input set to a list using list()
listOfSet = list(inputSet)
# Removing the last element using slicing i.e, taking all the elements except the last
resultList = listOfSet[:-1]
# Converting the result to a set
resultSet = set(resultList)
# printing the resultant set after removing the last element
print("Given Set after Removing Last Element", resultSet)

# input set containing strings
inputSet_1 = {"Hello", "Tutorialspoint", "python"}
print("The Given Set is:", inputSet_1)
# similarly removing the last element "python" from the set
# using list(),set() and slicing
resultSet_1 = set(list(inputSet_1)[:-1])
# printing the resultant set after removing the last element
print("Given Set after Removing Last Element", resultSet_1)

Output

On execution, the above program will generate the following output −

The Given Set is: {1, 2, 5, 7, 8}
Given Set after Removing Last Element {1, 2, 5, 7}
The Given Set is: {'Tutorialspoint', 'Hello', 'python'}
Given Set after Removing Last Element {'Tutorialspoint', 'Hello'}

Conclusion

We covered how to remove the last element of a set using four distinct methods in this article. We also learned how to use the pop() function to remove a random element from a set, as well as how to convert a set to a list and vice versa, as well as how to slice it.

Updated on: 27-Jan-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements