Python Set remove() – Remove Element

The Python set remove() function is used to remove a single element from the Set. The Set in python is a one-dimensional data structure that will not allow duplicate entries. If you try to add any existing element in the set, It will not return any error and it will not add an element. If you try to remove an element that doesn’t exists in the Set will return an error.

1. Quick Examples of Set remove()

Following are quick examples of how to remove an element from a Set.


# Quick Examples

# Consider the set with integers
myset=set({12,32,6,56,78,90,54,67})
print(myset)

# Remove 56 from the set
myset.remove(56)
print("After removing 56: ",myset )

# Consider the set with strings
myset=set({"hello","welcome","to","sparkby","examples"})
print(myset)

# Remove "to" from the set
myset.remove("to")
print("After removing to: ",myset )

# Remove "hello" from the set
myset.discard("to")
print("After removing hello: ",myset )

2. Python Set remove()

The set.remove() method of Python will remove a particular element from the set. At a time, it will remove only a single element. This method takes the 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.

2.1 Set remove() – Syntax


# Set remove() syntax - here myset is the input set
myset.remove(element)

4. Remove Element From Python Set

Let’s create a Python set with integer values and remove an element from the set. As I said above, you can remove only one element at a time from the Set. In order to remove multiple elements you need to call the remove() method multiple times.


# Consider the set with integers
myset=set({12,32,6,56,78,90,54,67})
print(myset)

# Remove 56 from the set
myset.remove(56)
print("After removing 56: ",myset)

# Remove 78 from the set
myset.remove(78)
print("After removing 78: ",myset)

# Remove 12 from the set
myset.remove(12)
print("After removing 12: ",myset)

# Output:
# {32, 67, 6, 12, 78, 54, 56, 90}
# After removing 56:  {32, 67, 6, 12, 78, 54, 90}
# After removing 78:  {32, 67, 6, 12, 54, 90}
# After removing 12:  {32, 67, 6, 54, 90}

Here, we created a set with 8 integers. First, we removed 56 and again we used remove() to eliminate 78 from the set.

5. Remove String Element From Set

Quickly, let’s also see an example with strings in the set.


# Consider the set with strings
myset=set({"hello","welcome","to","sparkby","examples"})
print(myset)

# Remove "examples" from the set
myset.remove("examples")
print("After removing examples: ",myset )

# Remove "to" from the set
myset.remove("to")
print("After removing to: ",myset )

# Output:
# {'hello', 'to', 'welcome', 'examples', 'sparkby'}
# After removing examples:  {'hello', 'to', 'welcome', 'sparkby'}
# After removing to:  {'hello', 'welcome', 'sparkby'}

Here, we created a set with 5 strings. After that, we removed “examples” and “to” strings separately from it.

6. Set discard()

When you remove an element that doesn’t exist by using remove(), you will get an error. To overcome this issue, if you wanted to have a silent delete without error use discard() function.

So let’s pass an element to the remove() method that is not existing in the set.


# Consider the set with integers
myset=set({12,32,6,56,78,90,54,67})

# Remove 100 from the set
myset.remove(100)
print("After removing 100: ",myset )

# Output:
# {32, 67, 6, 12, 78, 54, 56, 90}
# After removing 56:  {32, 67, 6, 12, 78, 54, 90}
python set remove
Error

You can see that KeyError is returned as 100 doesn’t present in the set.

If you don’t want to display the error, you can use discard() method, instead of remove(). It is similar to remove(), But it will not return any error if the specified element is not present in the set.

6.1 Set discard() – Syntax


# Set discard() syntax - here myset is the input set
myset.discard(element)

6.2 discard() Example


# Consider the set with integers
myset=set({12,32,6,56,78,90,54,67})

# Remove 100 from the set
myset.discard(100)
print("After removing 100: ",myset )

# Output:
# After removing 100:  {32, 67, 6, 12, 78, 54, 56, 90}

100 doesn’t exist in the set and the error is not returned by discard().

4. Conclusion

We have seen how to remove an element from the set using remove() method in python. It will return an error if the specified element is not found in the set, So in order to overcome this error, we used discard() method. You can use discard() instead of remove() in these scenarios.

Leave a Reply