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_update() method with examples

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

In the last tutorial, we discussed Set difference() method, which returns the difference between two given Sets. In this guide, we will see difference_update() method which doesn’t return anything but it updates the first Set with the Set difference. For example, calling method like this A.difference_update(B) would update the Set A with the A-B (elements that are in Set A but not in Set B).

Python Set difference_update() Syntax

X.difference_update(Y)

This would update the Set X with the X-Y
X-Y: Elements that are only in Set X and not in Set Y

Parameters: This method accepts a Set as a parameter.
Return Value: None. This method doesn’t return anything.

Python Set difference_update() Example

In the following example we have two Sets X and Y. We are calling X.difference_update(Y) which updates the Set X with the Set difference X – Y.

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

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

# calling difference_update() method
X.difference_update(Y)

# Displaying Set X and Y
print("X is:", X)
print("Y is:", Y)

Output:
Python difference_update() method example

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 union() 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

Comments

  1. Prakhar says

    November 25, 2019 at 4:06 PM

    What is the difference between X.difference_update(Y) and X.difference(Y).

    Reply

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