Python Set Remove Methods

python-set-remove-ipcisco

In this lesson, we will focus on removing the items in a Python Set. We will see Python Set Remove methods with different coding examples. Before this, let’s learn all the methods used for this purpose.

 

There are different Python Methods to remove an item in a set. These python methods are given below:

 

  • Remove method
  • Discard method
  • Pop method

 

Beside these methods, there are also another method and a function that do this job. But they delete all the items. So what are these method and function. They are given below:

 

  • Clear method
  • Del Function

 


You can also check Python Set Add Method


 

Clear method deletes all the items in a python set.

Del function deletes the complete python set.

 

To learn these methods better, let’s give coding example for each of them.

 

You can Watch the video of this lesson!


You can also check Python Set Difference Method


Remove Method

 

The first method used to remove a python set item is remove method. With this method, we can remove a specific item in a python set. We can give the item that we sould like to remove as an item. There can be only one item for remove method.

 

In the below example, we will remove “Balin” from the set.

 

dwarves1 = {"Thorin", "Balin", "Dwalin"}
dwarves1.remove("Balin")
print(dwarves1)

 

The output of this python code will be:

 

{'Dwalin', 'Thorin'}

 

python-set-remove-ipcisco

 

Again, the below example will remove

 

numbers = {10,45,2,6,36}
numbers.remove(45)
print(numbers)

 

The output of this  python code will be like below:

 

{2, 36, 6, 10}

 


 

Discard Method

 

Like remove method, discard method is also used fort he same purpose. The usage of this method is also similar to remove method.

 

 

dwarves1 = {"Thorin", "Balin", "Dwalin"}
dwarves1.discard("Balin")
print(dwarves1)

 

{'Dwalin', 'Thorin'}

 

python-set-discard-ipcisco

 

The below exmaple is also a number set example for discard method usage.

 

numbers = {10,45,2,6,36}
numbers.discard(45)
print(numbers)

 

{2, 36, 6, 10}

 


 

Pop Method

 

The last method that is used as python set remove method and remove one item of a python set is pop method. But pop method is a little risky. Because it removes the last item in the set. But we do not know the last member of any set, it is random. So, when we use python pop method with a set, we remove any item randomly.

 

 

Let’s check the below example. When we run the below example;

 

dwarves1 = {"Thorin", "Balin", "Dwalin"}
dwarves1.pop()
print(dwarves1)

 

Everytime we run the code, the output will change and remove one of the items randomly.

 

{'Thorin', 'Dwalin'}

 

python-set-pop-ipcisco

 


 

Clear Method

 

Python set clear method is used to remove all the items in a set. With this method, we do not use any parameters and when we use this method with a set, all the items in the set is removed.

 

dwarves1 = {"Thorin", "Balin", "Dwalin"}
dwarves1.clear()
print(dwarves1)

 

set()

 

python-set-clear-ipcisco

 

This methos is also sued in list to remove all the list items. But there is no such method for tuples.

 


 

Del Function

 

Del function is the function which deletes the complete set. After this command there is nothing about previously defined set.

 

dwarves1 = ("Thorin", "Balin", "Dwalin")
del dwarves1
print(dwarves1)

 

python-set-del-funtion-ipcisco

 

After the above python code, we will receive an error as output that says there is no such item (dwarves1).

 

In this lesson, we have learned three different methods that works as python set remove method. We have also learned another method named clear method, that deletes all the items in the set. Beside, we have seen del function, that deletes the complete set. You can check the other methods of sets in this Python Programming Course.

Back to: Python Programming Course > Python Sets

Leave a Reply

Your email address will not be published. Required fields are marked *

Python Programming Course

Collapse
Expand
Latest Blog Posts