What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Python Set remove() Method Explained

  • Sep 18, 2024
  • 6 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Kusum Jain
Python Set remove() Method Explained

A set in Python is an unordered collection of unique elements. Today, we will explore various techniques to remove items from a set in Python with examples.

Removing a Single Element from a Set in Python

A set is created using curly braces {} or the set() function. It automatically eliminates duplicate elements, ensuring that only distinct values are stored. This makes it an excellent choice for tasks such as removing duplicates from a list or checking for the presence of unique items.

To remove a single item from a set, Python provides the remove() and discard() methods. Both methods accomplish the same goal: eliminating a specific element from the set. However, they differ in handling scenarios when the item does not exist in the set.

  1. The remove() method: This method removes the specified element from the set. If the element is not found, it raises an KeyError exception.

my_set = {1, 2, 3, 4, 5}
my_set.remove(3)
print(my_set)  

 

 

Output:

 

{1, 2, 4, 5}

 

 

If we try to remove an item that doesn't exist:

 

my_set.remove(6)

 

 

Output: Raises a KeyError


   2. The discard() method: This method removes the specified element from the set if it exists. If the element is not present, no error is raised, and the set remains unchanged.

 

my_set = {1, 2, 3, 4, 5}
my_set.discard(3)
print(my_set)  

 

Output:

{1, 2, 4, 5}

 

Even if the element doesn't exist, discard() will not raise an exception:

my_set.discard(6)  

 

No error, the set remains unchanged.

Removing Multiple Elements from a Set

There may be cases where you need to remove multiple elements from a set at once. Python provides several methods to achieve this.

  1. Using the -= operator: You can utilize the -= operator to remove multiple elements by specifying another set or an iterable containing the elements to be removed.

my_set = {1, 2, 3, 4, 5}
my_set -= {2, 4}
print(my_set)  

 

Output:

{1, 3, 5}

 

The -= operator removes all elements present in the given set or iterable.

     2. Using the difference_update() method: This method removes all elements found in the provided set or iterable from the original set.

my_set = {1, 2, 3, 4, 5}
my_set.difference_update({2, 4})
print(my_set)  

 

 Output:

{1, 3, 5}

 

The difference_update() the method modifies the set in place, unlike the -= operator.

Now, coming to complexity. both the `remove()` and `discard()` methods have an average time complexity of O(1). This means that the time taken to remove an element from a set does not depend on the size of the set.

The O(1) complexity is achieved through the use of hash tables, which are the underlying data structure for sets in Python. Hash tables provide constant-time lookup, insertion, and deletion operations, making them highly efficient for handling sets with a large number of elements.

Regardless of the size of the set, the `remove()` and `discard()` methods locate the element to be removed using its hash value, perform the removal operation, and update the internal data structure accordingly. This process takes a constant amount of time, regardless of the number of elements in the set.

Conclusion

Today, we explained the various techniques available in Python to remove items from a set. We covered removing a single element using the remove() and discard() methods, as well as removing multiple elements with the -= operator and the difference_update() method. 

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Kusum Jain
I am an avid coder on CodeChef, I partake in hackathons regularly. I am an independent and self-motivated student with a love for coding and experiencing new things.