• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Set Operations in Python

Set Operations in Python

Author: Aditya Raj
Last Updated: November 2, 2021

Sets are container objects that contain unique elements in it. In this article, we will look at various set operations like union, intersection, and set difference. We will also implement the set operations in python.

Union Operation

If we are given two sets A and B, the union of both sets is calculated as a set that contains elements from both the set A and set B. We denote a set union operation using a ∪  sign.

If we have a set C that is union of A and B, we can write C= A ∪ B.  For example, suppose that we have set A={1, 2, 3, 4, 5 } and set B = {2, 4, 6,8},  then the set C= A ∪ B will contain elements {1, 2, 3, 4, 5, 6, 8 }. You can observe that each element in C either belongs to A or belongs to B or belongs to both A and B. 

We can implement the set union operation in python using the union() method. The union() method when invoked on a set A takes another set B as input argument and returns the set formed by union of A and B. This can be observed as follows.

A = {1, 2, 3, 4, 5}
B = {2, 4, 6, 8}
print("Set A is:", A)
print("Set B is:", B)
C = A.union(B)
print("Union of A and B is:", C)

Output:

Set A is: {1, 2, 3, 4, 5}
Set B is: {8, 2, 4, 6}
Union of A and B is: {1, 2, 3, 4, 5, 6, 8}

We can perform a union of more than two sets at the same time. In such a situation, the set created from union operation contains the elements from each of the sets that are participating in the union operation. For example, if we have set A= {1,2,3,4,5}, set B= {2,4,6,8,10} and set C={ 7,8,9,10} then the resultant set D= A ∪ B ∪ C will contain elements {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }.

We can perform union of more than two sets in python by providing other sets as input to the union() method when it is invoked on a single set as follows.

A = {1, 2, 3, 4, 5}
B = {2, 4, 6, 8}
C = {7, 8, 9, 10}
print("Set A is:", A)
print("Set B is:", B)
print("Set C is:", C)
D = A.union(B, C)
print("Union of A, B and C is:", D)

Output:

Set A is: {1, 2, 3, 4, 5}
Set B is: {8, 2, 4, 6}
Set C is: {8, 9, 10, 7}
Union of A, B and C is: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Intersection Operation

If we are given two sets A and B, the intersection of both sets is calculated as a set that contains those elements that are present in both the set A and set B. We denote a set intersection operation using a ∩  sign.

If we have a set C that is intersection of A and B, we can write C= A ∩ B.  For example, suppose that we have set A={1, 2, 3, 4, 5, 6 } and set B = {2, 4, 6,8},  then the set C= A ∩ B will contain elements {2, 4, 6 }. You can observe that each element in C belongs to both A or and B. 

We can implement the set intersection operation in python using the intersection() method. The intersection() method when invoked on a set A takes another set B as input argument and returns the set formed by intersection of A and B. This can be observed as follows.

A = {1, 2, 3, 4, 5, 6}
B = {2, 4, 6, 8}
print("Set A is:", A)
print("Set B is:", B)
C = A.intersection(B)
print("Intersection of A and B is:", C)

Output:

Set A is: {1, 2, 3, 4, 5, 6}
Set B is: {8, 2, 4, 6}
Intersection of A and B is: {2, 4, 6}

We can perform an intersection of more than two sets at the same time. In such a situation, the set created from intersection operation contains the elements that are present in every set participating in the intersection operation. For example, if we have set A= {1,2,3,4,5,6}, set B= {2,4,6,8,10} and set C={ 2,4,7,8,9,10} then the resultant set D= A ∩ B ∩ C will contain elements {2, 4}.

We can perform intersection of more than two sets in python by providing other sets as input to the intersection() method when it is invoked on a single set as follows.

A = {1, 2, 3, 4, 5, 6}
B = {2, 4, 6, 8, 10}
C = {2, 4, 7, 8, 9, 10}
print("Set A is:", A)
print("Set B is:", B)
print("Set C is:", C)
D = A.intersection(B, C)
print("Intersection of A, B and C is:", D)

Output:

Set A is: {1, 2, 3, 4, 5, 6}
Set B is: {2, 4, 6, 8, 10}
Set C is: {2, 4, 7, 8, 9, 10}
Intersection of A, B and C is: {2, 4}

Set Difference Operation

If we are given two sets A and B, the difference of set A and set B is calculated as a set that contains those elements that are present A but are not present in the set B. We denote a set difference operation using a –  sign.

If we have a set C that is the difference of A and B, we can write C= A – B.  For example, suppose that we have set A={1, 2, 3, 4, 5, 6 } and set B = {2, 4, 6,8},  then the set C= A – B will contain elements {1, 3, 5 }. You can observe that each element in C belongs to both A but does not belong to B. 

We can implement the set difference operation in python using the difference() method. The difference() method when invoked on a set A takes another set B as input argument and returns the set formed by difference of A and B. This can be observed as follows.

A = {1, 2, 3, 4, 5, 6}
B = {2, 4, 6, 8}
print("Set A is:", A)
print("Set B is:", B)
C = A.difference(B)
print("Difference of A and B is:", C)

Output:

Set A is: {1, 2, 3, 4, 5, 6}
Set B is: {8, 2, 4, 6}
Difference of A and B is: {1, 3, 5}


Conclusion

In this article, we have discussed different set operations. We have also implemented them in python using sets. To learn more about python programming, you can read this article on list comprehension. You may also like this article on the linked list in Python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us