Open In App

Python – Remove multiple elements from Set

Last Updated : 15 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 inbuilt 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}

Method #3: Using the intersection() method

step-by-step approach: 

1.Define two sets, my_set and remove_set.

2.Find the intersection of the two sets using the intersection() method. This returns a new set that contains only the elements that are common between the two sets.

3.Use the -= operator to remove the elements in the common_set from my_set. This modifies my_set in place.

4.Print the updated my_set using the print() function.

Python3




my_set = {1, 2, 3, 4, 5}
remove_set = {3, 4, 5}
 
# Get a new set that contains only the elements that are common between my_set and remove_set
common_set = my_set.intersection(remove_set)
 
# Remove the common elements from my_set using the -= operator
my_set -= common_set
 
# Print the updated set
print(my_set)


Output

{1, 2}

time complexity: O(n)

space complexity: O(m)

Method #4:Using filter() and set():

Algorithm:

1.Initialize a set and a list of elements to remove.
2.Use the filter() method to filter out the elements that are present in the list of elements to remove from the original set.
3.Convert the filtered elements to a set using the set() method.
4.The resulting set will have the elements that are not present in the list of elements to remove.

Python3




# 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 filter() and set() to remove multiple elements
test_set = set(filter(lambda x: x not in rem_ele, test_set))
 
# printing result
print("Set after removal : " + str(test_set))
#This code is contributed by Jyothi pinjala


Output

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

The time complexity : O(n), where n is the size of the original set. This is because the filter function iterates over the set once to remove the elements that match the given condition. The set function has a time complexity of O(n) as well, where n is the number of elements in the resulting set.

The space complexity : O(n), where n is the size of the original set. This is because the filter function creates a new iterable containing the elements that satisfy the given condition. The set function also creates a new set with the remaining elements.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads