Python Remove Set Element If Exists

How to remove an element from Python Set by checking if an element exists? If you want to remove the element only if it exists in the set. You can use the python inbuilt functions like remove() and discard() to remove the element and use if condition to check if an element exists.

  1. If the element exists, then the if condition becomes true and statements inside the if condition will be executed.
  2. If the element doesn’t exist, then statements inside the else block are executed.

1. Remove Set Element if Exists

The set.remove() method of Python will remove a particular element from the set. This method takes the single element you wanted to remove from the set as an argument, if the specified element does not exist in the set, then “KeyError” is returned. To overcome the error either you can use the exception handling or use the if condition to check.


# Create a set
myset1 = {12,90,43,56,"Hello"}
print("Set: ",myset1)

# Remove 12 if it exists in the set()
# using remove()
removing_item=12

if removing_item in myset1:
  myset1.remove(removing_item)
  print(removing_item,"exists in the set and is removed")
  print("Final Set: ",myset1)
else:
  print(removing_item,"doesn't exists in the set")

# Output:
# Set:  {43, 12, 'Hello', 56, 90}
# 12 exists in the set and is removed
# Final Set:  {43, 'Hello', 56, 90}

The element 12 exists in the set, so the condition inside the if block is executed. Hence 12 is removed from the set.

Example 2: Let’s consider set of elements and try to remove “python” from it.


# Remove "python" if it exists in the set()
# using remove()
removing_item='python'

if removing_item in myset1:
  myset1.remove(removing_item)
  print(removing_item,"exists in the set and is removed")
  print("Final Set: ",myset1)
else:
  print(removing_item,"doesn't exists in the set")

# Output:
# Set:  {43, 12, 'Hello', 56, 90}
# python doesn't exists in the set

The element “python” doesn’t exist in the set, so the condition inside the else block is executed.

2. 2. Remove Set Element if Exists using discard()

The set.dicard() method of Python will remove a particular element from the set. It is similar to remove() method, But it will not return any error of the element not found. Let’s use this method with the example above.


# Create a set
myset1 = {12,90,43,56,"Hello"}
print("Set: ",myset1)

# Remove 12 if it exists in the set()
# using discard()
removing_item=12

if removing_item in myset1:
  myset1.discard(removing_item)
  print(removing_item,"exists in the set and is removed")
  print("Final Set: ",myset1)

else:
  print(removing_item,"doesn't exists in the set")

# Output:
# Set:  {43, 12, 'Hello', 56, 90}
# 12 exists in the set and is removed
# Final Set:  {43, 'Hello', 56, 90}

The 12 exists in the set. So the condition inside the if block is executed. Hence 12 is removed from the set.

Let’s remove the element that doesn’t exist in the Python set.


# Remove "python" if it exists in the set()
# using discard()
removing_item='python'

if removing_item in myset1:
  myset1.discard(removing_item)
  print(removing_item,"exists in the set and is removed")
  print("Final Set: ",myset1)

else:
  print(removing_item,"doesn't exists in the set")

# Output:
# Set:  {43, 12, 'Hello', 56, 90}
# python doesn't exists in the set

The “python” doesn’t exist in the set, so the condition inside the else block is executed.

4. Difference between remove() & discard()

remove()discard()
remove() is a method that will raise an error if we try to remove an element that doesn’t exists in an iterable.discard() is a method that will not raise an error if we try to remove an element that doesn’t exist in an iterable.
It is better to use a try-except block while using the remove() method or handle the exception.There is no need to handle exceptions.

5. Conclusion

In this article, you have learned how to remove the set element if exists in Python by using remove() and discard() methods with examples. Also, we discussed the exact differences between these two methods.

Leave a Reply