×

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

One Liner If-Else Statements in Python

Before learning one liner if else Python, let us first know what Python is. Python is a broadly helpful, object-oriented and dependable language having a large number of utilizations from...

4 minutes read.

Python History

Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. Python language is known for its features. Some features are given below: SimplisticFree and open...

4 minutes read.

Python Linked List

Linked List Linked lists non-contiguous linear data structure which is made up of nodes and used to store value and a pointer pointing to the next node. It is linked with...

6 minutes read.

How to clear screen in Python?

How to clear screen in python There are times when we execute our program and it results in an unexpected output. The obtained result can contain some kind of garbage values...

4 minutes read.

Python Skyline

Python Programming Language: 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...

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.

How to Install Pandas in Python?

Pandas is a library created in Python for handling and analyzing data. Pandas provides a variety of procedures and data structures for manipulating time series and numerical data.  Pandas is...

3 minutes read.

How to write a program in python?

How to write a program in python? In python, writing programs is not a big deal. What we need to work on is our logic and some basic rules of this...

4 minutes read.

Nested Tuple in Python

If you are a Python learner, this page contains all the information that helps you know about nested tuple and how to access it in python. What is a Tuple? Multiple items...

4 minutes read.

Self in Python

 “self" is neither a keyword nor has a special meaning in Python, but it has a place and a job to do in Object-oriented programming. When we create a class...

6 minutes read.

Standard Scaler in SKLearn

The sci kit learns in python is a library thatch is used in machine learning which is used to work on data modeling.It is only focused on the data modeling,...

4 minutes read.

Mrcnn 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...

6 minutes read.

Continue and Pass Statements in Python

Difference between continue and pass statements in Python The tasks can be repeated and automated efficiently using loops in Python. Sometimes we have to exit from the entire loop completely, we...

2 minutes read.

Python Set intersection() method

Python Set intersection() method The set.intersection() method in Python returns a new set with elements that are similar between two or more sets. Syntax set.intersection(set1, set2 ... etc) Parameter set1- This parameter represents the set to search for...

2 minutes read.

How to comment out a block of code in Python

When you are writing code in Python, you may want to document it. You need to mention why a piece of code functions, for instance. Mathematics, algorithms, and intricate business...

4 minutes read.

Python String Variable

Python programming language: 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...

4 minutes read.

Python Modules

Python Modules The Python module is a collection of definition and statements. It can contain functions, classes, and variables.  Combining the related code into a module makes the code easier to understand and use....

5 minutes read.

Number Pattern Programs in Python

Learning Python is very easy, and it understands in better way compared to other programming languages. One of the many reasons why it has emerged as the most widely used...

4 minutes read.

How to reverse a string in python

How to reverse a string in python A Brief About Strings- The String is a data type in Python that has a sequence of characters. This series of characters are represented in...

4 minutes read.

Python Dictionary keys() method

Python Dictionary keys() method The dictionary.keys() method in Python returns a view object that displays a list of all the keys in the dictionary Syntax dictionary.keys() Parameter NA Return This method returns a view object that displays...

2 minutes read.