• 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 Difference in Python

Set Difference in Python

Author: Aditya Raj
Last Updated: May 5, 2022

Sets are used to store unique objects. Sometimes, we might need to find the elements in a set that are not present in another given set. For this, we use the set difference operation. In this article, we will discuss what is set difference is. We will also discuss approaches to find the set difference in python.

Table of Contents
  • What is the Set Difference?
  • How to Find The Set Difference in Python?
  • Find Set Difference Using The difference() Method in Python
  • Conclusion

What is the Set Difference?

When we are given two sets A and B. The set difference (A-B) is a set consisting of all the elements that belong to A but are not present in set B. 

Similarly, the set difference (B-A) is a set consisting of all the elements that belong to B but are not present in set A. 

Consider the following sets.

A={1,2,3,4,5,6,7}

B={5,6,7,8,9,10,11}

Here, set A-B will contain the elements 1,2,3, and 4 as these elements are present in set A but do not belong to set B. Similarly, set B-A will contain the elements 8,9,10,11 as these elements are present in the set B but do not belong to set A .

Let us now discuss approaches to find set difference in python. 

How to Find The Set Difference in Python?

Given the sets A and B, if we want to find the the set difference A-B, we will first create an empty set named output_set. After that, we will traverse set A using a for loop. While traversal, we will check for each element if they are present in the set B or not. If any element in set A doesn’t belong to the set B, we will add the element to the output_set using the add() method. 

After execution of the for loop, we will get the set difference A-B in the output_set. You can observe this in the following example.

A = {1, 2, 3, 4, 5, 6, 7}
B = {5, 6, 7, 8, 9, 10, 11}
output_set = set()
for element in A:
    if element not in B:
        output_set.add(element)
print("The set A is:", A)
print("The set B is:", B)
print("The set A-B is:", output_set)

Output:

The set A is: {1, 2, 3, 4, 5, 6, 7}
The set B is: {5, 6, 7, 8, 9, 10, 11}
The set A-B is: {1, 2, 3, 4}

If we want to find the the set difference B-A, we will traverse set B using a for loop. While traversal, we will check for each element if they are present in the set A or not. If any element in set B doesn’t belong to the set A, we will add the element to the output_set using the add() method. 

After execution of the for loop, we will get the set difference B-A in the output_set. You can observe this in the following example.

A = {1, 2, 3, 4, 5, 6, 7}
B = {5, 6, 7, 8, 9, 10, 11}
output_set = set()
for element in B:
    if element not in A:
        output_set.add(element)
print("The set A is:", A)
print("The set B is:", B)
print("The set B-A is:", output_set)

Output:

The set A is: {1, 2, 3, 4, 5, 6, 7}
The set B is: {5, 6, 7, 8, 9, 10, 11}
The set B-A is: {8, 9, 10, 11}

Find Set Difference Using The difference() Method in Python

Python provides us with the difference() method to find the set difference. The difference() method, when invoked on set A, takes  set B as input argument, calculates the set difference, and returns a set containing the elements in the set (A-B). You can observe this in the following example.

A = {1, 2, 3, 4, 5, 6, 7}
B = {5, 6, 7, 8, 9, 10, 11}
output_set = A.difference(B)
print("The set A is:", A)
print("The set B is:", B)
print("The set A-B is:", output_set)

Output:

The set A is: {1, 2, 3, 4, 5, 6, 7}
The set B is: {5, 6, 7, 8, 9, 10, 11}
The set A-B is: {1, 2, 3, 4}

Conclusion

In this article, we have discussed how to find the set difference in python. To learn more about sets, you can read this article on set comprehension in python. You might also like this article on list comprehension 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