In this Python tutorial, we’ll learn about Python set remove method. Python code examples will be used to practically demonstrate the usage of remove method in Python set.
Introduction: Python Set Remove Method
As the name suggests, the remove method is used to remove a specific item from Python set. We’ll first understand its syntax. After that, we’ll understand its practical implementation using Python code examples.
Syntax of Remove Method
set.remove(item)
- Python set remove method takes a single argument. This is the specific element that will be removed from existing Python set.
- This method returns None. It just updates the existing Python set by removing a specific item from it.
- It raises a key error excepting if the item passed as an argument to this method is not present in the set.
Implementing Python Set Remove Method (Multiple Examples)
Below examples will cover the use of remove method in Python set.
Example 1: Remove Item Using Python Set Remove Method
val={8,6,9,'Usman',(5,7)} val.remove(9) print(val)
Output
{6, 8, (5, 7), 'Usman'}
We can see that the item (9) has been removed from set. We can also remove Python tuple from set. See below code:
val.remove((5,7)) print(val)
Output
{6, 8, 9, 'Usman'}
If there are duplicate items in set then they both will be removed(if remove method has that specific item as an argument) as using that specific set automatically remove the duplicate item and leave only one occurence of that item. See below:
v={8,6,9,6} v.remove(6) print(v)
Output
{8, 9}
Example 2: Removing A Non Existing Item From Set
a={'Zeeshan',23,7,8.4} a.remove(56) print(a)
Output
KeyError: 56
We can see that removing an item which does not exist in the specified Python set will result in a key error.
Example 3: Remove Method Return Type
b={'a','b','c',5,7,8} returnedValue=b.remove('c') print(returnedValue)
Output
None
We’ve created a Python set and have removed the item (‘c’) from that set. We’ve stored the return value of remove method in a variable. Finally, we’ve printed its value and it shows None. It means this remove method just remove the specific item from set and does not return anything.
Let’s now print this set as well and see if it gets updated or not. See below:
print(b) # we're using the same above code here as well
Output
{'a', 'b', 5, 7, 8}
We can see that the item has been successfully removed from this set.
Example 4: Using Remove Method With Python For Loop
b={1,2,3,4,5,6,7,8,9,10,'a','b','c'} for var in b.copy(): if var!='a': b.remove(var) print(b)
Output
{'a'}
- First we’ve created a Python set.
- After that, we’ve created a Python for loop which will take each and every item of set while iterating over it.
- Then we’ve used a condition using Python if statement which specifies that do nothing if the value is equal to string ‘a’. If not equal to string ‘a’ then remove that specific item from set.
- We’ve used a copy of set as removing item from set while iterating over it will raise a run time error. Click here to learn more about it.
So in this way we can easily utilize the Python set remove method. If you like this post and have learned new things from it then do not hesitate to share it with your team of Python developers.
Do give this method a try and share your experience with us.
We also encourage you to ask any questions you have regarding the implementation of Python set remove method. We’ll gladly provide answers to all your questions.
Conclusion
To wrap up our Python tutorial, we’ve learned the usage of Python set remove method in detail. We’d be glad to receive your insightful feedback on this article.
You can learn more regarding Python programming by visiting our other posts. Thank you for visiting this article.