×

Python Set difference_update() method

Python Set difference_update() method

The set.difference_update() method in Python removes the items that exist in both sets.

Syntax

set.difference_update(set1)

Parameter

set- This argument represents a set (minuend)

set1- This arguments represents a set(subtrahend)

Return

None

Example 1

# Python program explaining
# the set.difference_update() method
# initializing the first set
set1 = {"apple", "banana", "cherry"}
print("First Set:",set1)
# initializing the second set
set2 = {"google", "microsoft", "apple"}
print("Second Set:",set2)
# removes the items that are common in both sets. 
set1.difference_update(set2) 
print("After calling the difference_update() method...")
# prinitng the first set method
print("First set:",set1)
# prinitng the second set method
print("Second set:",set2)

Output

First Set: {'cherry', 'banana', 'apple'}
Second Set: {'apple', 'microsoft', 'google'}
After calling the difference_update() method...
First set: {'cherry', 'banana'} 
Second set: {'apple', 'microsoft', 'google'} 

Example 2

# Python program explaining
# the set.difference_update() method
#initializing a set with alphabets A to Z
Alphabets = {'a', 'b', 'c', 'd','e',
   'f','g','h','i',
   'j','k','l','m',
   'n','o','p','u', 
    'v','w','x','y','z'}
print("Alphabets:",Alphabets)
 # initializing set2 with vowels 
vowels = {'a', 'e', 'i','o','u'}
print("vowels: ",vowels)
# removing the common elements from the Alphabet set
# Alphabets- vowels= Consonants 
print("After subtracting the common values from Alphabets set... ")
Alphabets.difference_update(vowels)
print("Consonants: ",Alphabets) 

Output

Alphabets: {'c', 'm', 'w', 'g', 'z', 'n', 'f', 'd', 'e', 'h', 'k', 'v', 'i', 'u', 'l', 'x', 'p', 'o', 'y', 'a', 'b', 'j'}
vowels:  {'u', 'e', 'o', 'a', 'i'}
After subtracting the common values from Alphabets set...
Consonants:  {'c', 'm', 'w', 'g', 'z', 'n', 'f', 'd', 'h', 'k', 'v', 'l', 'x', 'p', 'y', 'b', 'j'} 

Example 3

# Python program explaining
# the set.difference_update() method
#initializing a set with numbers from 1 to 10
Numbers = {1,2,3,4,5,6,7,8,9,10} 
print("Numbers:",Numbers)
 # initializing set2 with even numbers
even = {2,4,6,8,10}
print("Even numbers: ",even) 
# subtracting the common even elements from Numbers
# Numbers- even=  Odd
print("After subtracting even numbers from Natural numbers... ")
Numbers.difference_update(even) 
print("Odd numbers: ",Numbers) 

Output

Numbers: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Even numbers:  {8, 10, 2, 4, 6}
After subtracting even numbers from Natural numbers... 
Odd numbers:  {1, 3, 5, 7, 9} 

Related Topics

Isreal() Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

4 minutes read.

Python oct() function

Python oct() function The oct() function in Python converts an integer number to an octal string prefixed with “0o”. Syntax oct(x) Parameter x: This parameter represents an Integer Number Return This function returns an octal string. Example 1 #...

1 minute read.

Python Set remove() method

Python Set remove() method The set.remove() method in Python removes the specified element from the set if it is present in the set else this method will raise an error if the specified item does...

2 minutes read.

Python Write Excel File

Python Write Excel File The Python xlwt module is used to write an excel file and perform multiple operations on it. It can be used to write text, numbers, and formulas for multiple...

3 minutes read.

Python Glob

Methods for matching files that include particular patterns in accordance with UNIX shell-related expansion rules are referred to as "glob" methods. Similar methods for finding, locating, and searching for all of...

12 minutes read.

Python str() function

Python str() function The str() function in Python converts the specified value into a string. Syntax class str(object='')           or class str(object=b'', encoding='utf-8', errors='strict') Parameter object:  This parameter represents any object to convert the given...

1 minute read.

Working with CSV files in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

7 minutes read.

Comments in the Python Programming Language

In this tutorial, we will learn how to comment in multiple lines and paragraphs in the python programming language. Commenting a Paragraph in Python Most people think the importance of comments is...

2 minutes read.

Python Super function

The super function is used to access and call the methods or functions involved in the parent class during Inheritance. What is Inheritance? The process where the parent class inherits some of...

3 minutes read.

Python Basics

In this tutorial, we will learn about the basics of Python, a very famous programming language. This tutorial will help you understand the language better if you start with python....

8 minutes read.

Python Tuple Methods

Python Tuple Methods Python has two built-in methods that are used for tuples. The following are the two methods: Method Description count() The tuple.count() method in Python returns the number of times a...

1 minute read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.

Subprocess in Python

In this tutorial, we will understand what is a subprocess in python and will understand how to use it. Subprocess A subprocess is a very prevailing portion of the python library. It...

3 minutes read.

Python Dictionary pop() method

Python Dictionary pop() method The dictionary.pop() method in removes the specified item and returns an element from a dictionary having the given key. Syntax dictionary.pop(key[, default]) Parameter key – This argument signifies the key which is to...

2 minutes read.

Python Stack

Python Stack: The work Stack is defined as arranging a pile of objects or items on top of another. It is the same method of allocating memory in the stack...

10 minutes read.

Python Fit Transform

In machine learning, fit (), transform(), fit transform () methods are provided by scikit learn package. This package is used in model fitting and data processing. These methods are implemented...

2 minutes read.

Python program to find the GCD or HCF

Python program to find the GCD or HCF The largest positive integer that perfectly divides the two numbers is the HCF (Highest Common Factor) or GCD (Greatest Common Deviser). For example,...

5 minutes read.

Create First GUI Application using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

Python Program to Print all the Prime Number in an Interval

Python Program to Print all the Prime Number in an Interval What is Prime numbers? A prime number is referred to those numbers that can be divisible by them only. In simple words,...

4 minutes read.

StandardScaler in Sklearn

When and How Should You Use StandardScaler? StandardScaler comes into play whenever the properties of the provided dataset change significantly within their ranges or are recorded in different measurement units. Since using...

4 minutes read.