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.



Previous Article
Next Article

Similar Reads

Remove multiple elements from a list in Python
Given a list of numbers, write a Python program to remove multiple elements from a list based on the given condition. Example: Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10] Input: [11, 5, 17, 18, 23, 50] Output: Remove = [1:5], New_list = [11, 50] Multiple elements can be deleted from a list in Python, based on the knowledge
4 min read
Python | Remove multiple keys from dictionary
While working with Python dictionaries, we can have a utility in which we require to remove more than one key at once. This type of problem can occur while working in the Web Development domain with NoSQL Databases. Let's discuss certain ways in which this task can be performed. Remove multiple keys from a dictionary using del Here, we are using a
6 min read
Python - Ways to remove multiple empty spaces from string List
Sometimes, while working with Python strings, we have a problem in which we need to perform the removal of empty spaces in Strings. The problem of filtering a single space is easier. But sometimes we are unaware of the number of spaces. This has applications in many domains. Let's discuss certain ways in which this task can be performed. Method #1
6 min read
Python - Append Multiple elements in set
In Python, sets are an unordered and mutable collection of data type what does not contains any duplicate elements. In this article, we will learn how to append multiple elements in the set at once. Example: Input: test_set = {6, 4, 2, 7, 9}, up_ele = [1, 5, 10]Output: {1, 2, 4, 5, 6, 7, 9, 10}Explanation: All elements are updated and reordered. (5
4 min read
How to remove rows from a Numpy array based on multiple conditions ?
In this article, we will learn how to remove rows from a NumPy array based on multiple conditions. For doing our task, we will need some inbuilt methods provided by the NumPy module which are as follows: np.delete(ndarray, index, axis): Delete items of rows or columns from the NumPy array based on given index conditions and axis specified, the para
3 min read
How to remove multiple selected items in listbox in Tkinter?
Prerequisite: Tkinter, Listbox in Tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. In t
2 min read
Python - Elements frequency count in multiple lists
Sometimes while working with Python lists we can have a problem in which we need to extract the frequency of elements in list. But this can be added work if we have more than 1 list we work on. Let's discuss certain ways in which this task can be performed. Method #1: Using dictionary comprehension + set() + count() This is one of the way in which
6 min read
Python - Filter rows with Elements as Multiple of K
Given a Matrix, extract rows with elements multiple of K. Input : test_list = [[5, 10, 15], [4, 8, 12], [100, 15], [5, 10, 23]], K = 4 Output : [[4, 8, 12]] Explanation : All are multiples of 4. Input : test_list = [[5, 10, 15], [4, 8, 11], [100, 15], [5, 10, 23]], K = 4 Output : [] Explanation : No rows with all multiples of 4. Method #1 : Using l
6 min read
Get Index of Multiple List Elements in Python
In Python, retrieving the indices of specific elements in a list is a common task that programmers often encounter. There are several methods to achieve this, each with its own advantages and use cases. In this article, we will explore some different approaches to get the index of multiple list elements in Python. Get Index Of Multiple List Element
3 min read
Locating multiple elements in Selenium Python
Locators Strategies in Selenium Python are methods that are used to locate single or multiple elements from the page and perform operations on the same. Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provide a simple API to write functional/acceptance tests using Selenium WebDriver. After one ha
5 min read
Practice Tags :
three90RightbarBannerImg