Skip to content

How to remove an element from a set in Python ( in 4 ways )

How to remove an element from a set in Python

In this tutorial we will learn how to remove an element from a set in Python . Python set has 4 built-in methods to do it . They are as follows –

  • Python Set pop() method
  • Python Set remove() method
  • Python Set discard() method
  • Python Set clear() method

Let us have a look about all this methods with examples –

Python Set pop() method

It removes and returns an arbitrary element from a given set . If the set is empty then it will raise KeyError .

Syntax of pop() method

set.pop()

Parameters of pop() method

This method does not have any parameter .

Return type of pop() method

It returns the removed element .

Examples of Python Set pop() method

Example 1: Remove an arbitrary element from a set in Python

# declaring a set
myset={"a","b","c","d","e"}

# deleting an arbitrary element
popped_element=myset.pop()

# printing the set
print(myset)

# Printing the removed element
print(popped_element)
{'b', 'a', 'c', 'e'}
d
  • In the above code first we have declared a set named myset as {"a","b","c","d","e"}
  • Then we have removed an arbitrary element from the set using pop() method and stored the returned element to a popped_element variable
  • Then we have printed the set and popped_element variable .
  • From the output we can see that an arbitrary element "d" is removed from the set .

Example 2: Try to remove an arbitrary element from an empty set

# declaring an empty set
myset=set()

# applying pop on the empty set
myset.pop()
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_5200/1066628389.py in <module>
      3 
      4 # applying pop on the empty set
----> 5 myset.pop()

KeyError: 'pop from an empty set'

In the above code as an empty set does not have any element so pop() can not remove any arbitrary element from the set and raised KeyError .

Python Set remove() method

It removes an specified element from a given set . If the element is not found then it will raise KeyError .

Syntax of remove() method

set.remove(element)

Parameters of remove() method

  • element = It is the element which will be removed from the set . If the element is not found then it will raise KeyError

Return type of remove() method

It returns None .

Examples of Python Set remove() method

Example 1: Remove a specified element from a set in Python

# declaring a set
myset={"a","b","c","d","e"}

# remove c from the set
myset.remove("c")
print(myset)

# remove d from the set
myset.remove("d")
print(myset)

# trying to remove c again
myset.remove("c")
{'d', 'b', 'a', 'e'}
{'b', 'a', 'e'}
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_5200/771185309.py in <module>
     11 
     12 # trying to remove c again
---> 13 myset.remove("c")

KeyError: 'c'
  • In the above code first we have declared a set named myset as {"a","b","c","d","e"}
  • After that we have removed "c" from the set using remove() method and printed the set . From the output we can see that "c" is removed from the set .
  • Then we have removed "d" from the set using remove() method and printed the set .
  • After that again we have tried to remove "c" from the set using remove() method . As "c" is not in the set so remove() method raised KeyError .

Python Set discard() method

It removes an specified element from a given set . If the element is not found then it will not do anything.

Syntax of discard() method

set.discard(element)

Parameters of discard() method

  • element = It is the element which will be removed from the set .

Return type of discard() method

It returns None .

Examples of Python Set discard() method

Example 1: Remove a specified element from a set in Python

# declaring a set
myset={"a","b","c","d","e"}

# remove c from the set
myset.discard("c")
print(myset)

# remove d from the set
myset.discard("d")
print(myset)

# trying to remove c again
myset.discard("c")
print(myset)
{'d', 'b', 'a', 'e'}
{'b', 'a', 'e'}
{'b', 'a', 'e'}
  • In the above code first we have declared a set named myset as {"a","b","c","d","e"}
  • After that we have removed "c" from the set using discard() method and printed the set . From the output we can see that "c" is removed from the set .
  • Then we have removed "d" from the set using discard() method and printed the set .
  • After that again we have tried to remove "c" from the set using discard() method and printed the set . As "c" is not in the set so discard() method has not done anything with the set .

Python set remove vs discard

Previously we have learn about python set remove() and python set discard() method . The main difference between Python set remove vs discard is If the element we want to remove from the set is not found then remove() will raise KeyError but discard() will not raise any error .

Python Set clear() method

It removes all elements from a given set .

Syntax of clear() method

set.clear()

Parameters of clear() method

This method does not have any parameter .

Return type of clear() method

It returns None .

Examples of Python Set clear() method

Example 1: Remove all elements from a set in Python

# declaring a set
myset={"a","b","c","d","e"}

# remove all elements from the set
myset.clear()

# printing the set
print(myset)
set()
  • In the above code first we have declared a set named myset as {"a","b","c","d","e"}
  • Then we have removed all elements from the set using clear() method .
  • After that we have printed the set . From the output we can see that an empty set is printed .

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply