Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Remove multiple elements from Set

  • Last Updated : 02 Feb, 2021

Given a set, the task is to write a Python program remove multiple elements from set.

Example:

Input : test_set = {6, 4, 2, 7, 9}, rem_ele = [2, 4, 8]
Output : {9, 6, 7}

Explanation : 2, 4 are removed from set.

Input : test_set = {6, 4, 2, 7, 9}, rem_ele = [4, 8]
Output : {2, 9, 6, 7}

Explanation : 4 is removed from set.

Method #1 : Using “-” operator 

In this, we perform task of eliminating elements using computing difference using “-” operator.

Python3




# Python3 code to demonstrate working of
# Remove multiple elements from Set
# Using "-" operator
  
# initializing set
test_set = {6, 4, 2, 7, 9}
  
# printing original set
print("The original set is : " + str(test_set))
  
# initializing remove elements
rem_ele = [2, 4, 8]
  
# using "-" operator to remove multiple elements
res = test_set - set(rem_ele)
  
# printing result
print("Set after removal : " + str(res))

Output:

The original set is : {2, 4, 6, 7, 9}
Set after removal : {9, 6, 7}

Method #2 : Using difference_update()

In this, we remove elements getting difference and updating the set using inbuild set method difference_update().

Python3




# Python3 code to demonstrate working of
# Remove multiple elements from Set
# Using difference_update()
  
# initializing set
test_set = {6, 4, 2, 7, 9}
  
# printing original set
print("The original set is : " + str(test_set))
  
# initializing remove elements
rem_ele = [2, 4, 8]
  
# using difference_update() to remove multiple elements
test_set.difference_update(set(rem_ele))
  
# printing result
print("Set after removal : " + str(test_set))

Output:

The original set is : {2, 4, 6, 7, 9}
Set after removal : {9, 6, 7}


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!