Python Set Methods

python-set-methods-ipcisco.com

There are different Python Set Methods used in python programming. Some of them are similar with list, tuple and dictionary methods. In this lesson we will focus on these python set methods one by one with different python code examples.

 

So what are the methods used with Python Sets? Some of the important ones are given below:

 

 

Now, let’s focus on each of them and give examples to learn the functions of these methods better.

 


You can also check Python Dictionary Methods


 

Python Set add Method

 

The first method that we will see here is python set add method. This method give us to add a new set item. Here, we will write the name of the new item as a parameter of add method.

 

dwarves = {"Thorin", "Balin", "Dwalin"}
dwarves.add("Gimli")
print(dwarves)

 

As you can see below, the new item has added to the set:

 

{'Balin', 'Gimli', 'Thorin', 'Dwalin'}

 

You can practice below!

 

python-set-methods-add-method

 

For example let’s use add method to add a new router family to our router set.

 

routers = {"Cisco 800", "Cisco ISR 4000", "Cisco ASR 1000"}
routers.add("Cisco ASR 9000")
print(routers)

 

As you can see below, the new router family has added to the router set:

 

{'Cisco ISR 4000', 'Cisco ASR 9000', 'Cisco ASR 1000', 'Cisco 800'}

 


You can also watch the video of this lesson!

 

Python clear Method

 

One of the other Python Set Methods is Clear method. Clear method is used to remove all the items in the list. In the below example, we will delete all the items in the list.

 

routers = {"Cisco 800", "Cisco ISR 4000", "Cisco ASR 1000"}
routers.clear()
print(routers)

 

As the output, we will receive an empty set. But there is still a set there, it is define and not deleted.

 

set()

 

python-set-methods-clear-method

 


 

Python remove Method

 

The other one of the Python Set Methods is remove method. Python set remove method is one of the three remove methods that is used for item delete. With this code, we can delete any items in the set.

 

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

 

As you can see below, the new item has added to the set:

 

{'Dwalin', 'Thorin'}

 

python-set-methods-remove-method

 

Again we can remove any router family in our routers set with remove method.

 

routers = {"Cisco 800", "Cisco ISR 4000", "Cisco ASR 1000"}
routers.remove("Cisco ISR 4000")
print(routers)

 

The output will be like below:

 

{'Cisco ASR 1000', 'Cisco 800'}

 


 

Python Set Methods: Python discard Method

 

We can use discard method as remove method to remove any item in a python set. To see  this let’s do an example.

 

Again we can remove any router family in our routers set with remove method.

 

routers = {"Cisco 800", "Cisco ISR 4000", "Cisco ASR 1000"}
routers.discard("Cisco ISR 4000")
print(routers)

 

The output will be like below:

 

{'Cisco ASR 1000', 'Cisco 800'}

 

python-set-methods-discard-method

 


 

Python pop Method

 

We use python pop method with sets also to remove an item in the set. But pop method removes the last item in the set. The last item can not known in a set. So, this remove process is done randomly. In other words, any of the set item is deleted randomly. By the way, for pop method, we so not use any parameter.

 

routers = {"Cisco 800", "Cisco ISR 4000", "Cisco ASR 1000"}
routers.pop()
print(routers)

 

Whenever we run this code, the output always change a random item is removed form the list. Below, you can see one of these outputs.

 

{'Cisco 800', 'Cisco ISR 4000'}

 

python-set-methods-pop-method

 


 

Python Set Methods: Set union Method

 

One of the other Python Set Methods is union method. Python set union method is used to combine one more sets together. Here the repeated items do not added to the new set. But all the items in evey set is added to this new set as items.

 

Below, we will combine two sets of numbers with union method.

 

numbers1 = {1,2,3}
numbers2 = {2,3,7,10,15}
print(numbers1.union(numbers2))

 

As you can see below, in the output, repeated numbers is not used.

 

{1, 2, 3, 7, 10, 15}

 

python-set-methods-union-method

 

We acn do the same job with the help of “|” operator.

 

numbers1 = {1,2,3}
numbers2 = {2,3,7,10,15}
print(numbers1 | numbers2)

 

The output of this code will be:

 

{1, 2, 3, 7, 10, 15}

 

Now, let’s use this unicon code for our network devices.

 

routers = {"Cisco 800", "Cisco ISR 4000", "Cisco ASR 1000"}
switches = {"Catalyst 3750", "Catalyst 4500"}
print(routers.union(switches))

 

In the output, we will see the two set items together in a single set. Here, there is no repeated items so there is no need to remove repeated items.

 

{'Catalyst 3750', 'Cisco ISR 4000', 'Cisco ASR 1000', 'Cisco 800', 'Catalyst 4500'}

 


 

Set intersection Method

 

Set intersection method is used to determined the common items that resides in two or more sets. In other words, with this methods we determine the common members between different sets.

 

Let’s show this with an example:

 

numbers1 = {1,2,3}
numbers2 = {2,3,7,10,15}
numbers3 = numbers1.intersection(numbers2)
print(numbers3)

 

The output of this python code will be:

 

{2, 3}

 

python-set-methods-intersection-method

 

Fort he same results, we can also use “&” operator.

 

numbers1 = {1,2,3}
numbers2 = {2,3,7,10,15}
print(numbers1 & numbers2)

 

The output of this python code will be the same as above output.

 

{2, 3}

 


 

Set difference() Method

 

Set difference method is used to find the differences of two or more sets. To understand this emthod better, you can check the below example.

 

numbers1 = {1,2,3}
numbers2 = {2,3,7,10,15}
print(numbers2.difference(numbers1))
{10, 15, 7}

 

python-set-methods-difference-method

 

Here, we print the items that resides in numbers2 but not numbers1 in a set.

 

numbers1 = {1,2,3}
numbers2 = {2,3,7,10,15}
print(numbers2-numbers1)

 

{10, 15, 7}

 


 

Set symetric_difference() Method

 

The set symmetic_diffence method is used to print the uncommon members of one more sets. In other words, with this method, we compare the sets and print the items that are not common to a new set.

 

Below examples will show this method usage.

 

numbers1 = {17,22,80}
numbers2 = {12,17,56,80,92}
print(numbers1.symmetric_difference(numbers2))

 

As you can see below, the output of this code will give us the items that are not common in both sets.

 

{22, 56, 12, 92}

 

python-set-methods-symmetric-difference-method

 

We can do the same job by using “^” operator.

 

numbers1 = {17,22,80}
numbers2 = {12,17,56,80,92}
print(numbers1^numbers2)

 

{22, 56, 12, 92}

 


 

Python copy() Method

 

As in other data types, python copy method is used to copy the sets also.

 

routers = {"Cisco 800", "Cisco ISR 4000", "Cisco ASR 1000"}
x=routers.copy()
print(x)
{'Cisco 800', 'Cisco ISR 4000', 'Cisco ASR 1000'}

 

 

python-set-methods-copy-method

 


 

Python Set isdisjoint() Method

 

We use set isdisjoint() method to compare the sets again. And according to the this, if it find any common item, it returns as False. If not, then it returns as True.

 

The below code will return False, becose there are common numbers between two sets:

 

numbers1 = {17,22,80}
numbers2 = {12,17,56,80,92}
print(numbers1.isdisjoint(numbers2))
False

 

python-set-methods-isdisjoint-method

 

 

The below code will return True, because there is no common items between python sets this time.

 

numbers1 = {3,4,5}
numbers2 = {12,17,56,80,92}
print(numbers1.isdisjoint(numbers2))
True

 


 

Python Set issubset() Method

 

With set issubset () method, we check if all the items in one set exist in the other one. To understant this method better, let’s do an example.

 

The output of the below python code will be True. Because all the items in numbers1 exist in numbers2.

 

numbers1 = {12,56,80}
numbers2 = {12,17,56,80,92}
print(numbers1.issubset(numbers2))
True

 

python-set-methods-issubset-method

 

As a second example, we will give the below example. But this time, all the items in numbers will not exist in numbers2.

 

numbers1 = {12,56,80,85}
numbers2 = {12,17,56,80,92}
print(numbers1.issubset(numbers2))

 

As  we expected, the output of this command will be False.

 

False

 


 

Python Set issuperset() Method

 

The last method that we will discuss in this Python Set Methods is python set issuperset method. This method is similar to the above method but this is reverse.  This time if all the items in the second set exist in the first set, then the code return True. If not, then the result is False.

 

numbers1 = {10,1,20,2,3}
numbers2 = {1,2,3}
print(numbers1.issuperset(numbers2))
True

 

 

 

python-set-methods-issuperset-method

 

But the below code will return False, because all the items of the second set is not in the first set.

 

numbers1 = {10,1,20,2,3}
numbers2 = {1,2,3,4}
print(numbers1.issuperset(numbers2))
False

 

In this Python Set Methods lesson, we have learned top methods used with Python Sets. You can do more practices on these Python Set Methods with different examples. And with more exmaples, you will learn each method better.

 

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