BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Set difference() method with examples

Last Updated: March 31, 2019 by Chaitanya Singh | Filed Under: Python Tutorial

The difference() method in Python returns the difference between two given sets. Lets say we have two sets A and B, the difference between A and B is denoted by A-B and it contains the elements that are in Set A but in Set B.

How the difference is calculated between two sets?

Lets say we have two Sets X and Y as follows:
X = {“hello”, 2, 5}
Y = {2, 9, “bye”}

Difference between two sets is denoted by – sign.

Elements that are in Set X but not in Set Y
X-Y = {“hello”, 5}

Elements that are in Set Y but not in Set X
Y-X = {9, “bye”}

Python Set difference() method Syntax

X.difference(Y)

This is equivalent to X-Y.

Parameters: This method takes a Set as parameter.
Return Value: It returns a Set that is a difference between two sets. For example X.difference(Y) would return a Set that contains the elements that are in Set X but not in Set Y.

Python Set difference() method Example

In the following example we have two sets X and Y. We are finding the difference between X & Y and Y & X using the difference() method.

# Set X
X = {"hello", 9, 10, "hi"}

# Set Y
Y = {9, "hi", 6, "BeginnersBook"}

# Equivalent to X - Y
print("X-Y is:",X.difference(Y))

# Equivalent to Y - X
print("Y-X is:",Y.difference(X))

Output:
Python difference() method example

Difference between Sets using – Operator

We can use the – operator on Sets. This works same as difference() method. Lets take the same example that we have seen above using – operator.

# Set X
X = {"hello", 9, 10, "hi"}

# Set Y
Y = {9, "hi", 6, "BeginnersBook"}

# Equivalent to X.difference(Y)
print("X-Y is:",X-Y)

# Equivalent to Y.difference(X)
print("Y-X is:",Y-X)

Output:

X-Y is: {10, 'hello'}
Y-X is: {'BeginnersBook', 6}

As you can see we got the same output.

Top Related Articles:

  1. Python Data Types
  2. Python Recursion
  3. Python Keywords and Identifiers with examples
  4. Python Constructors – default and parameterized
  5. Python Set update() method with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap