Pop multiple elements python – Python Set: Remove Single or Multiple Elements from a Set?

POP multiple elements python: A Python set is a list of things that are not in any particular order. – element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means we can change them after they’ve been formed.

Unlike other Python sets, the elements of the set have no index, which means we can’t access any element of the set directly using the index. We may, however, print them all together or loop through the collection to get the list of elements.

Examples:

Removing single element from the set

Input:

givenset= {'this', 'is', 'BTechGeeks'} element='is'

Output:

{'BTechGeeks', 'this'}

Removing multiple elements from the set

Input:

givenset= {'Hello' ,'this', 'is', 'BTechGeeks'} element=['is' , 'this ']

Output:

{'BTechGeeks', 'hello'}

Remove one or more elements from a set

There are several ways to remove one or more elements from a set some of them are:

Method #1:Using discard() function to remove single element

In Python, the built-in method discard() removes an element from a collection only if it is already present. If the element is missing from the list, no error or exception is thrown, and the original set is printed.

Implementing the discard() function if the element is present in the set:

# given set
givenset = {'this', 'is', 'BTechGeeks'}
# given element
element = 'is'
# removing element which is in set using discard function.
givenset.discard(element)
# printing the set
print(givenset)

Output:

{'BTechGeeks', 'this'}

Implementing the discard() function if the element is not present in the set:

# given set
givenset = {'this', 'is', 'BTechGeeks'}
# given element
element = 'hello'
# removing element which is not in set using discard function.
givenset.discard(element)
# printing the set
print(givenset)

Output:

{'is', 'this', 'BTechGeeks'}

Method #2:Using remove() function to remove single element

In Python, the built-in method remove() removes an element from a set only if it is already present, similar to how the discard() method removes an element from a set. If the element is missing from the list, an error or exception is thrown.

Implementing the remove() function if the element is present in the set:

# given set
givenset = {'this', 'is', 'BTechGeeks'}
# given element
element = 'is'
# removing element which is in set using remove() function.
givenset.remove(element)
# printing the set
print(givenset)

Output:

{'BTechGeeks', 'this'}

Implementing the remove() function if the element is not present in the set:

# given set
givenset = {'this', 'is', 'BTechGeeks'}
# given element
element = 'hello'
# removing element which is not in set using remove() function.
givenset.remove(element)
# printing the set
print(givenset)

Output:

Traceback (most recent call last):
  File "/home/ebe45b71f9f465d547ea9cb944ebe071.py", line 6, in <module>
    givenset.remove(element)
KeyError: 'hello'

Method #3:Using pop() function to remove single element

This operation takes an arbitrary element from the set and returns it.
It throws a KeyError if there are no elements to delete.

Implementing the pop() function if the element is present in the set:

# given set
givenset = {'this', 'is', 'BTechGeeks'}
# removing random element which is  in set using pop() function.
givenset.pop()
# printing the set
print(givenset)

Output:

{'is', 'BTechGeeks'}

Method #4:Using difference_update() to remove multiple elements from the set

The Set class in Python has a function called difference update() that takes a sequence as an argument and removes all of the elements from the set. Let’s use this to remove all of the list’s elements from the set.

Below is the implementation:

# given set
givenset = {'hello', 'this', 'is', 'BTechGeeks'}
# given list of elements to be deleted from set
elementslist = ['this', 'is']
# removing elements
givenset.difference_update(elementslist)
# printing the set
print(givenset)

Output:

{'BTechGeeks', 'hello'}

Method #5:Using discard() + for loop to remove multiple elements from set

Iterating over the elements to be removed and removing them one by one from the set  using discard() is another way to remove several elements from a set.

Below is the implementation:

# given set
givenset = {'hello', 'this', 'is', 'BTechGeeks'}
# given list of elements to be deleted from set
elementslist = ['this', 'is']
# Traverse the elementslist which is to be deleted
for element in elementslist:
    # remove the element from set using discard
    givenset.discard(element)
# printing the set
print(givenset)

Output:

{'BTechGeeks', 'hello'}

Method #6:Using remove() + for loop to remove multiple elements from set

Iterating over the elements to be removed and removing them one by one from the set  using remove() is another way to remove several elements from a set.

Below is the implementation:

# given set
givenset = {'hello', 'this', 'is', 'BTechGeeks'}
# given list of elements to be deleted from set
elementslist = ['this', 'is']
# Traverse the elementslist which is to be deleted
for element in elementslist:
    # remove the element from set using remove
    givenset.discard(element)
# printing the set
print(givenset)

Output:

{'hello', 'BTechGeeks'}

Related Programs: