34. Problem to remove multiple elements from a set

In this Python set program, we will remove multiple elements from a set with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Remove multiple elements from a set:

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the set.
3. Use a for loop to iterate over the second set.
4. Use an if statement to check whether the element is in the first set.
5. If yes then remove that element from the first set using remove() function.
6. Print the set.

				
					a = {1, 2, 4, 5, 7, 8, 9}
print("Original set: ",a)
b = {7,8,9}
for ele in b:
    if ele in a:
        a.remove(ele)
print("New set: ",a)
				
			

Output :

				
					Original set:  {1, 2, 4, 5, 7, 8, 9}
New set:  {1, 2, 4, 5}

				
			

Related Articles

add multiple elements to a set.

check if a set is empty.

Leave a Comment