How do you remove an element from a Set in Python

remove an element from a Set in python

Hello Techies,

In this tutorial, we will learn about How do you remove an element from a Set in Python with the help of examples.

In my previous blog, I covered the How to Remove Dictionary elements topic.

Let’s check different ways to remove an element from a Set in Python one by one.

Ways to Remove an element from a Set in Python

Set is an unordered collection data type that is mutable. Because sets are unordered, we cannot access items using indexes.

Let’s try to understand the set methods that help to remove an element from a Set in Python. Below are the methods:

  • pop()
  • remove()
  • discard()

pop()

This built-in Python function helps to pop out elements from the set. The pop() method removes a random element from the set.

Syntax of the set() method is:

set.pop()

The set() method takes no parameters.

Example 1: How do you pop an element from a set in Python?

# Program for remove an element from a Set in python
# using pop() method

# Creating set
data = {'test1', 'test2', 'test3', 'test4'}
print('Original set data:', data)

# pop element
data.pop()
print('Updated Set is:', data)

Output:

Original set data: {'test2', 'test4', 'test3', 'test1'}
Result: {'test4', 'test3', 'test1'}

Example 2: If the set is empty, a TypeError will be returned

# Program for remove an element from a Set in python
# using pop() method

# Creating set
data = {}
print('Original set data:', data)

# pop element
data.pop()
print('Updated Set is:', data)

Output:

Original set data: {}
Traceback (most recent call last):
  File "C:\Users\Pranali\Desktop\test\test1.py", line 7, in <module>
    data.pop()
TypeError: pop expected at least 1 argument, got 0

remove()

The remove() method is used to remove the specified element from the set.

Syntax of the remove() method is:

set.remove(element)

The remove() method takes the element as a parameter and removes it from the set.

Example 1: Remove an Element From The Set

# Program for remove an element from a Set in python
# using remove() method

# Creating set
data = {'test1', 'test2', 'test3', 'test4'}
print('Original set data:', data)

# remove element
data.remove('test2')
print('Updated Set is:', data)

Output:

Original set data: {'test3', 'test4', 'test2', 'test1'}
Updated Set is: {'test3', 'test4', 'test1'}

Example 2: Deleting a non-existent element from a set using the remove() method

# Program for remove an element from a Set in python
# using remove() method

# Creating set
data = {'test1', 'test2', 'test3', 'test4'}
print('Original set data:', data)

# remove element
data.remove('test12')
print('Updated Set is:', data)

Output:

Original set data: {'test1', 'test3', 'test4', 'test2'}
Traceback (most recent call last):
  File "C:\Users\Pranali\Desktop\test\test1.py", line 7, in <module>
    data.remove('test12')
KeyError: 'test12'

If non-existing element passed to remove() method, KeyError exception is raised.

If you do not want this error, then you can use discard() method. Let’s check How do you use discard() method in Python?


discard()

The discard() method is used to remove the specified element from the set, but this method does not raise the error if the specified element is not in the set.

Syntax of the discard() method:

s.discard(element)

The discard() method takes the element as a parameter and removes it from the set.

Example 1: How to use the discard() method in python?

# Creating set
data = {'test1', 'test2', 'test3', 'test4'}
print('Original set data:', data)

# remove element
data.discard('test3')
print('Updated Set is:', data)

Output:

Original set data: {'test2', 'test3', 'test4', 'test1'}
Updated Set is: {'test2', 'test4', 'test1'}

Example 2: Deleting a non-existent element from the set using the discard() method

# Creating set
data = {'test1', 'test2', 'test3', 'test4'}
print('Original set data:', data)

# remove element
data.discard('test13')
print('Updated Set is:', data)

Output:

Original set data: {'test4', 'test3', 'test1', 'test2'}
Updated Set is: {'test4', 'test3', 'test1', 'test2'}

How to remove multiple elements from a set?

Method 1: Using the ‘-‘ operator we can remove multiple elements from the set

# Program to remove multiple elements from a set

# Creating set
data = {'test1', 'test2', 'test3', 'test4'}
print('Original set data:', data)

# remove element list
rem_list = ['test3', 'test1']

# remove element
result = data - set(rem_list)
print('Updated Set is:', result)

Output:

Original set data: {'test2', 'test1', 'test3', 'test4'}
Updated Set is: {'test2', 'test4'}

Method 2: Using difference_update()

# Program to remove multiple elements from a set

# Creating set
data = {'test1', 'test2', 'test3', 'test4'}
print('Original set data:', data)

# remove element list
rem_list = ['test4', 'test2']

# remove element
data.difference_update(set(rem_list))
print('Updated Set is:', data)

Output:

Original set data: {'test3', 'test4', 'test1', 'test2'}
Updated Set is: {'test3', 'test1'}

Delete items from a set while iterating over it

Method 1: Using while loop

# Program to Delete items from a set while
# iterating over it

data = {'test1', 'test2', 'test3', 'test4'}
while data:
    data.pop()
    print(data)
print("Final result", data)

Output:

{'test2', 'test1', 'test4'}
{'test1', 'test4'}
{'test4'}
set()
Final result set()

Method 2: Using condition with list comprehension

# Program to Delete items from a set while
# iterating over it

data = {'test1', 'test2', 'test3', 'test4'}
print("Original set", data)
rem_list = ['test2', 'test4']
result = set([each for each in data if each not in rem_list])
print("Final result", result)

Output:

Original set {'test3', 'test2', 'test1', 'test4'}
Final result {'test3', 'test1'}

Interview Question:
Q:     What is difference between Discard() and Remove() function in python 3 
          sets?
Ans:  If non-existing element passed to remove() method, KeyError exception is 
          raised whereas The discard() method does not raise the error if the  
          specified element is not in the set.

Conclusion:

In this tutorial, you learned about Remove an element from a Set in python, We have covered the following points:

  • Set pop() method in python
  • Set remove() method in python
  • Set discard() method in python
  • How to remove multiple elements from a set?
  • Delete items from a set while iterating over it

Leave a Comment