×

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

Front end in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The...

4 minutes read.

Python Redis Example

Introduction: Redis, representing Distant Word reference Server, is a kind of key-esteem NoSQL server. Redis is intended to help circle capacity for determination, holding information after power is stopped, and stores information...

5 minutes read.

Pillow Python introduction and setup

Introduction Advanced image processing implies handling the picture carefully with the assistance of a PC. Utilizing picture handling we can perform activities like upgrading the picture, obscuring the picture, separating text...

4 minutes read.

How to build a Virtual Assistant Using Python

What is a virtual assistant? A virtual assistant is a new and very interesting concept in today’s world. When we hear the word “Virtual Assistant”, we can easily visualize “Jarvis or...

8 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 float()

Python float() class The float() class in Python returns a floating point number constructed from a number or string x. Syntax class float([x]) Parameter x: This parameter represents a number or a string that can be converted...

1 minute read.

Python Multiprocessing Processor

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Importing Numpy in Pycharm

Numpy : Numpy is one of the important library in python. The abbreviation of numpy is “Numerical python” or “Numeric Python”. This library is mainly used to access arrays. Numpy was created...

5 minutes read.

Python Syntax

Python is a strong object-oriented programming language that is simple to learn. Python was created to be a very readable programming language. The syntax of the Python programming language is...

8 minutes read.

Polymorphism in Python

What is polymorphism and why is it important? Polymorphism's literal definition is the state of occurring in diverse shapes or forms. When it comes to programming, the idea of polymorphism is crucial....

3 minutes read.

Python String upper() method

Python String upper() method The string.upper() method in Python returns a copy of the string converted to uppercase. Syntax string.upper() Parameter NA Return This method returns a copy of the string converted to uppercase. Example 1 # Python...

1 minute read.

Python Num2words

Python: 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 is said...

3 minutes read.

How to assign values to variables in Python and other languages?

Python makes it simple to construct variables. The value to be stored in the variable should then be written after a suitable name for the variable and the equality sign....

3 minutes read.

Python Word Tokenizer

Python Overview Python is an Object-Oriented high-level language. Python is designed to be highly beginner-friendly. Python has an English-like syntax, which is very easy to read. Python is an interpreted language...

4 minutes read.

How to Plot Graphs Using Python?

In this article, we are going to learn about how to plot different types of graphs using Python. We are going to use different approaches for every type of graph....

3 minutes read.

Python print() function

Python print() function The print() function prints the specified message to the screen or other standard output devices. Syntax print(*objects,sep=' ',end='\n',file=sys.stdout,flush=False) Parameter objects: This function represents an object, which will be converted to a string...

1 minute read.

Application to Search Installed Application using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Data Structures and Algorithms Using Python | Part 1

Data Structures: Data Structure is defined as a way to organize and store the data so that we can access the data and work more efficiently. Data structures also describe the...

18 minutes read.

Loan Calculator using PyQt5 in Python

In the following tutorial, we will learn how to build a Loan Calculator application using the PyQt5 library in the Python programming language. So, let's get started. Introduction to the code: The heading...

4 minutes read.

Character Set in Python

A collection of legal characters that will recognize by a programming language known as a Character set. Taking python programming language as an instance. The set of characters supported by the...

6 minutes read.