Python

Remove method in set

Click for All Topics

Remove method in set - Python

  • The remove method is used to delete specified elements from the set.
  • This method takes exactly one argument as an element of a set that you want to remove.
  • When you call the remove method and pass an element as an argument it first searches the element in the set and then removes it.
  • When we call the remove method with an element that is not present in the set, it will raise an error keyError.
  • This method doesn’t return anything.

 

Syntax –

set_name.remove(element)

 

Parameter –

  • element → the set element which you want to remove.

 

Example 1 – let’s see an example

				
					my_set={1,2,3,4,5}
print(my_set)
my_set.remove(5)
print(my_set)

# Output -
# {1,2,3,4,5} # my_set before removing element from set
# {1,2,3,4}   # my_set after removing element(5) from set
				
			
  • In the above example ,first we create a set with elements 1,2,3,4,5 and store it in the my_set variable.
  • After creating the set we print my_set and then remove 5 from my_set using the remove method.
  • Finally, we print the modified set on the console.

Example 2- trying to remove an element that do not exist

				
					set1={'e','d','S','l','a','s','h'}
print(set1)
set1.remove('o')  # will raise an error

# Output -
# {'e','d','S','l','a','s','h'}
# Traceback (most recent call last):
  # File "/home/main.py", line 3, in <module>
    # set1.remove('o') 
# KeyError: 'o'
				
			

Example 3- trying to give two arguments let’s see

				
					s={14,8,19,25,29,11}
print(s)
s.remove(8,19)  # will raise an error

# Output -
# {14,8,19,25,29,11}
# Traceback (most recent call last):
 # File "/home/main.py", line 3, in <module>
    # s.remove(8,19)
# TypeError: set.remove() takes exactly one argument (2 given)
				
			

if you have to remove multiple elements from the set then you have to call the remove method multiple times.

Let’s see the example –

				
					s={11,22,33,44,55,66,77,88,99}
print(s)
s.remove(11)
print(s) 
s.remove(44)
print(s)

# Output - 
# {11,22,33,44,55,66,77,88,99}  # before removing the element from the set
# {22,33,44,55,66,77,88,99}     # set after removing 11
# {22,33,55,66,77,88,99}        # set after removing 44
				
			

Example 4-

				
					s={'hello','everyone','this','is','edSlash'}
print(s)
return_value=s.remove('hello')
print(s)
print(return_value)

# Output - 
# {'hello','everyone','this','is','edSlash'}  # original set
# {'everyone','this','is','edSlash'}          # modified set
# None                                        # return value of remove method
				
			
  • In the above example, we can see that the remove method returns nothing.

Example 5- Removing odd element from the set

				
					s={1,2,3,4,5,6,7,8,9}
s1=s.copy()
print("original set",s)
print("copied set:",s1)
for i in s1:
    if i%2==1:
        s.remove(i)
print(s)

# Output -
# original set: {1, 2, 3, 4, 5, 6, 7, 8, 9}
# copied set: {1, 2, 3, 4, 5, 6, 7, 8, 9}
# modified original set: {2, 4, 6, 8}
				
			

What is the difference between the remove() and discard() methods in set Python?

 

remove()discard()
It will raise an error if an element is not present in the setIt will not give an error whether an element is present in the set or not
It is beneficial when you know the element of the setIt is ideal when you have no idea about the element of the set