Python Set Operations

python-set-difference-operation

In Python set are used for different Python Set Operations. There are different operation types used with python sets. In this lesson, we will focus on these operations and we will learn how to use them. We will see when we can use these Python Set Operations in our codes.

 

So what are these operations and operators used with them. These are:

 

  • “|” Operator
  • “&” Operator
  • “-“ Operator
  • “^“ Operator

 

 

| Operator – Python Set Union Method

 

The first operator used with Python sets is “|” operator. With this operator, we combine two sets together. We can do the same job with also python union method.

 

Below, we will combine two numbers set 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}

 

python-set-operations-union

 

The same code can be written with python union method like below:

 

You can also watch the video of this lesson.

 

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

 

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

 

python-set-union-operation

 

Again, we can use this operator to combine string sets like beloew:

 

dwarves1 = {"Thorin", "Balin", "Dwalin"}
dwarves2 = {"Gimli", "Bofur", "Thorin"}
print(dwarves1 | dwarves2)

 

As the output, we will see the combination of these sets. As you can see below, the double items will be only once in the new set. In other words, “Thorin” will be only one time in this new set.

 

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

 

 

& Operator – Python Set Intersection Method

 

The other operator used with Python sets is “&” operator. With this operator, we can find the common items in two sets together. We can do the same job with also python intersection method.

 

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

 

The output of this python code will be like beloe because, the common items in both sets are 2 and 3.

 

{2, 3}

The below strin set example will be give also the common item of the sets.

 

dwarves1 = {"Thorin", "Balin", "Dwalin"}
dwarves2 = {"Gimli", "Bofur", "Thorin"}
print(dwarves1 & dwarves2)

 

{'Thorin'}

 

python-set-intersection-operation

 

– Operator – Python Set Difference Method

 

“-“ operator is used to find the difference of two sets with if it is used with sets. The same job is done with difference method used in python. For example if we would like to find the items only in set1 but not in set2, we use (set1-set2)  code. Let’s given an example to understand better.

 

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

 

{10, 15, 7}

 

python-set-operations-difference

 

In this python code, we have get the difference of two sets. The output of this code shows that the items reside in numbers2 but not in numbers1. So, the common items has removed in the new set.

 

We can do the similar thing with difference method also.

 

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

 

 

 

If we use numbers1-numbers2, this time the outputput will be different. And the usage of difference method will be also changed.

 

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

 

 

{1}

 

 

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

 

 

^ Operator – Python Set Symetric Difference Method

 

^ Operator is used to find the symmetric difference of two sets. Fort he same job, we can use symmetric_difference method of python. So what is symmetric difference? Symettric difference gives us the combinateion of the items in both set that are not common in these sets. In other words, it returns with the items that are not reside in both sets at the same time.

 

 

In the below, example, we will use ^ Operator and print the items that are not common in both of the sets.

 

numbers1 = {17,22,80}
numbers2 = {12,17,56,80,92}
print(numbers1^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-symmetric-difference-operation

 

We can do the same job by using symmetric_difference method.

 

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

 

 

The below usage will also give the same result. But the items in the set can be in different order.

 

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

 

{12, 22, 56, 92}

python-set-operations-symmetric-difference

Back to: Python Programming Course > Python Sets

Leave a Reply

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

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k