Removing Elements from the Set in Python

Below are some of the ways by which we can remove elements from the set in Python:

  • Using remove() Method or discard() Method
  • Using pop() Method
  • Using clear() Method

Using remove() Method or discard() Method

Elements can be removed from the Sets in Python by using the built-in remove() function but a KeyError arises if the element doesn’t exist in the hashset Python. To remove elements from a set without KeyError, use discard(), if the element doesn’t exist in the set, it remains unchanged.

Python3
# Creating a Set
set1 = set([1, 2, 3, 4, 5, 6,
            7, 8, 9, 10, 11, 12])
print("Initial Set: ")
print(set1)

# Removing elements from Set  using Remove() method
set1.remove(5)
set1.remove(6)
print("\nSet after Removal of two elements: ")
print(set1)

# Removing elements from Set using Discard() method
set1.discard(8)
set1.discard(9)
print("\nSet after Discarding two elements: ")
print(set1)

# Removing elements from Set using iterator method
for i in range(1, 5):
    set1.remove(i)
print("\nSet after Removing a range of elements: ")
print(set1)

Output
Initial Set: 
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

Set after Removal of two elements: 
{1, 2, 3, 4, 7, 8, 9, 10, 11, 12}

Set after Discarding two elements: 
{1, 2, 3, 4, 7, 10, 11, 12}

Set after Removing a range of elements: 
{7, 10, 11, 12}

Using pop() Method

Pop() function can also be used to remove and return an element from the set, but it removes only the last element of the set. 

Note: If the set is unordered then there’s no such way to determine which element is popped by using the pop() function. 

Python3
# Python program to demonstrate
# Deletion of elements in a Set

# Creating a Set
set1 = set([1, 2, 3, 4, 5, 6,
            7, 8, 9, 10, 11, 12])
print("Initial Set: ")
print(set1)

# Removing element from the
# Set using the pop() method
set1.pop()
print("\nSet after popping an element: ")
print(set1)

Output
Initial Set: 
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

Set after popping an element: 
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

Using clear() Method

To remove all the elements from the set, clear() function is used. 

Python3
#Creating a set
set1 = set([1,2,3,4,5])
print("\n Initial set: ")
print(set1)


# Removing all the elements from 
# Set using clear() method
set1.clear()
print("\nSet after clearing all the elements: ")
print(set1)

Output
 Initial set: 
{1, 2, 3, 4, 5}

Set after clearing all the elements: 
set()

Python Sets

Python Set is an unordered collection of data types that is iterable, mutable, and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements. The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set. Here, we will see what is a set in Python and also see different examples of set Python.

Similar Reads

Creating a Set in Python

Python Sets can be created by using the built-in set() function with an iterable object or a sequence by placing the sequence inside curly braces, separated by a ‘comma’....

Adding Elements to a Set in Python

Below are some of the approaches by which we can add elements to a set in Python:...

Accessing a Set in Python

Set items cannot be accessed by referring to an index, since sets are unordered the items has no index. But you can loop through the Python hash set items using a for loop, or ask if a specified value is present in a set, by using the in keyword....

Removing Elements from the Set in Python

Below are some of the ways by which we can remove elements from the set in Python:...

Frozen Sets in Python

Frozen sets in Python are immutable objects that only support methods and operators that produce a result without affecting the frozen set or sets to which they are applied. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation....