×

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

Python Linear regression

In this tutorial, we will understand the meaning, usage, and types of Linear Regression. Further, we will comprehend the terms cost function and Optimization. Linear Regression is the widely used Machine...

3 minutes read.

Python Web Development projects

What is Python? Python is currently one of the most widely used computer programming languages. This isn't just an exaggeration; Python is the second-most famous programming language on the software development...

3 minutes read.

Loan calculator using Tkinter in Python

Tkinter: Tkinter, part of all common Python distributions, is the de facto method for creating Graphical User Interfaces (GUIs) in Python. The only framework included in the Python standard library is...

4 minutes read.

Confusion Matrix Visualization Python

The confusion matrix is a two-dimensional array that compares the anticipated and actual category labels. These are the True Positive, True Negative, False Positive, and False Negative classification categories for...

4 minutes read.

How to change the names of Columns in Python

Introduction To play with huge amounts of data, in Python we require a tool. The tool which is available in Python is Pandas. Pandas is an open-source library. It is used...

3 minutes read.

Python Parser

Introduction : Parsing is referred to as the processing and translation of a Python program into machine language. In general, we could indeed say that the command parse is used to...

4 minutes read.

Static Variables in Python

What is a Static Variable? The variable that remains with a constant value throughout the program or throughout the class is known as a " Static Variable ". Static variables are...

3 minutes read.

Python sorted() function

Python sorted() function The sorted() function returns a sorted list of the specified iterable object. Syntax sorted(iterable, *, key=None, reverse=False) Parameter iterable: It is a required parameter that represents the sequence to sort, list, dictionary, tuple etc. key:...

1 minute read.

Python Pascal Triangle

Python Pascal Triangle Pascal triangle A pascal triangle is a number pattern of triangular array of the binomial coefficients. For designing a pascal triangle, we write a function in the program which...

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

Convert XML to JSON in Python

XML conversion is very useful if we work on an API that returns data in JSON format and the source of data is in XML format. JSON A JSON file reserves the...

4 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 String islower() method

Python String islower() method The string.islower() method returns a boolean value true if all cased characters in the string are lowercase and for  any other value is returns false otherwise. Syntax string.islower() Parameter NA Return This...

1 minute read.

Scrimba python

Scrimba allows you to study whenever and wherever the topics or concepts you want. It also replaces classroom instruction with interactive screencasts, live events, and student-to-student help. Scrimba is an interactive...

3 minutes read.

Python Assert

Python Assert Python provides an assert statement which is used to check the logical expression. If the given logical expression is true, then it precedes for the next line; otherwise, it raises an...

2 minutes read.

Python Project Ideas for Beginners

Python project ideas for beginners Advanced Python is an amazing platform to grow and achieve success as a developer in the future. Python is a programming language that can be very...

7 minutes read.

Python dir() function

Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax dir([object]) Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function...

2 minutes read.

Python List index() method

Python List index() method The list.index () method in Python returns the position at the first occurrence of the specified value. Syntax list.index(x[, start[, end]]) Parameter element – This parameter represents the element whose lowest index will be returned. start (Optional)...

2 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 Parse Text File

We will learn different ways of read text records in Python. TL;DR The accompanying tells the best way to read all texts from the readme.txt document into a string: with open('readme.txt') as f: lines...

5 minutes read.