beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Python Set difference_update() method with examples

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

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

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap