How to Remove Item from Set in Python? [Using 7 Methods]

Do you want to remove the element from a set? In this Python tutorial, I will explain “How to remove item from set in Python” using different methods with some illustrative examples.

To remove an item from a set in Python, we can use the remove() or discard() method to take the item name that we want to remove, as an argument and remove them. We can also use the pop() method to remove any element from a set or set comprehension to create a new set having all the elements from the original set except the elements to remove.

When I was working with sets in Python, I got a requirement to remove item from set in Python. Let me explain all the methods that I got to know during my research.

We can also use some alternative ways to remove elements from a set in Python, like:

  1. Using filter function
  2. Using difference() function
  3. The – operator

1. Remove element from set Python using the remove() function

The Python set remove() method is used to remove a specific element from the set. The remove() function takes a single element as an argument and removes it from the set.

If the element is not found in the set, then this throws a KeyError in Python. So, before using this method make sure the element you want to remove is present in the Python set.

Here is an instance, to remove the element from a set in Python:

Cities = {"New York", "Los Angeles", "Chicago", "Miami", "Seattle"}
City_to_remove = "Miami"
Cities.remove(City_to_remove)
print("Cities without", City_to_remove + ":", Cities)

Output:

Major Cities without Miami: {'Chicago', 'New York', 'Los Angeles', 'Seattle'}

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

How to Remove Item from Set in Python

The error message we will get is mentioned below:

How to remove element from set using iterator in Python

2. Python remove element from set using the discard() function

The discard() function in Python is also used to remove a specific element from a set. We also have to pass the item name as an argument in the discard() function.

But if the element is not present in the set in Python, then it does nothing, and the set remains unchanged.

Let’s see an example of this:

parks = {"Yellowstone", "Grand Canyon", "Yosemite", "Zion"}
park_to_be_removed = "Yosemite"
parks.discard(park_to_be_removed)
print("National Parks without", park_to_be_removed + ":", parks)

Output:

National Parks without Yosemite: {'Yellowstone', 'Zion', 'Grand Canyon'}

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

remove an element from a set and return the set in Python

3. Python remove item from set using the pop() function

A random element can be removed from the set and returned using the pop() function in Python. It is hard to know which element will be removed because Python sets are unordered.

Let’s take an example of this:

states = {"California", "Texas", "New York", "Florida", "Ohio"}
random_state = states.pop()
print("Randomly Selected State:", random_state)
print("Remaining States:", states)

Output:

Randomly Selected State: Texas
Remaining States: {'California', 'Florida', 'Ohio', 'New York'}

After executing the code in Pycharm, one can see the output in the below screenshot.

How to remove the last element in a set in Python

4. How to delete elements in a set using the filter function

The filter() function allows us to create a new set in Python with elements that satisfy some conditions applied as a function.

To remove an item from a set in Python, we can use filter() with the lambda function to create a set without the specific elements.

states = {"California", "Texas", "New York", "Florida", "Ohio"}
filtered_states = set(filter(lambda x: 'a' not in x.lower(), states))
print("States without 'a':", filtered_states)

Output: Here, we have filtered the set elements that don’t have the alphabet ‘a’, using the lower() function in the lambda function.

States without 'a': {'Ohio', 'New York'}

Below is the screenshot mentioned, which is taken after the execution of the code.

remove several elements from a set in Python

5. Remove items from set in Python using set comprehension

We can also use a set comprehension to create a new set without the elements we want to remove. This method allows us to specify conditions to remove the element from the set in Python.

Here is the code to use set comprehension in Python to remove the element from a set.

states = {"California", "Texas", "New York", "Florida", "Ohio"}
filtered_states = {x for x in states if 'i' not in x.lower()}
print("States without 'i':", filtered_states)

Output:

States without 'i': {'New York', 'Texas'}

Here is the screenshot after the code implementation:

How to remove an element from set Python

6. Remove Item from Set in Python using the difference function

The difference() method returns a new set in Python that will contain the elements that differ between the two sets. we can pass the item name in a set as an argument to the difference function.

Here is the instance to remove item from set in Python using the difference() function.

time_zones = {"Eastern", "Central", "Mountain", "Pacific", "Hawaii"}
filtered_time_zones = time_zones.difference({"Hawaii"})
print("The filtered time zone are:", filtered_time_zones)

Output: We can pass multiple elements to the set in the arguments of the difference function in Python.

The filtered time zone are: {'Pacific', 'Central', 'Mountain', 'Eastern'}

Down we have provided the screenshot of the code:

Remove items from Python set

7. How to remove elements from a set using – operator in Python

The – operator in Python can also be used for the set difference as well. This function returns a new set without the elements to be removed.

Here is an instance to remove item from set in Python using the difference operator:

presidents = {"Washington", "Lincoln", "Roosevelt", "Cleveland", "Grant"}
non_consecutive_term = {"Cleveland"}
remaining_presidents = presidents - non_consecutive_term
print("Presidents without non-consecutive term:", remaining_presidents)

Output: Again we can provide any number of elements in the set to be removed in Python.

Presidents without non-consecutive term: {'Grant', 'Washington', 'Lincoln', 'Roosevelt'}

After running the code in the Pycharm editor, we have taken the below screenshot:

How to Remove an Element From a Set in Python

Conclusion

This article explains how to remove item from set in Python using seven different methods such as remove(), discard(), pop(), set comprehension, filter with lambda function, and difference or the – operator. Each method has its properties.

The choice of the methods depends upon the requirements of the program. Learning all the methods can help one to implement them in their code easily.

You may also like to read: