×

Python Set difference() Method

Python Set difference() Method

The set.difference() method in Python returns the set difference of two sets(A-B).

Syntax

set.difference(set1)

Parameter

set- This argument represents a set (minuend)

set1- This arguments represents a set(subtrahend)

Return

This method returns the difference of the two specified sets i.e. the returned set will contain the items that exist only in the first set, and not in both sets.

Example 1

# Python program explaining 
# the set.difference() 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}
print("Even numbers: ",even)
# Equivalent to A-B 
# Numbers- even=  Odd
print("After subtracting even numbers from Natural numbers... ")
print("Odd numbers: ",Numbers.difference(even)) 

Output

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

Example 2

# Python program explaining
# the set.difference() 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)
# Equivalent to A-B 
# Alphabets- vowels= Consonants
print("After subracting vowels from Alphabets... ")
print("Consonants: ",Alphabets.difference(vowels)) 

Output

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


Related Topics

How to print in the same line in Python?

How to print in the same line in python By default, the print function in Python takes us to the next line and prints the desired statement in the output. In this...

5 minutes read.

Python Dictionary fromkeys() method

Python Dictionary fromkeys() method The dictionary.fromkeys() method in Python creates a new dictionary from the given sequence of elements and returns a dictionary with the specified keys and values. Syntax dictionary.fromkeys(sequence[, value]) Parameter sequence- This...

1 minute read.

Python System Requirements

Introduction As we know, Python is a popular programming language usually used to write scripts for operating systems. It’s handy adequate for utilizing in web development and application design. In this article,...

2 minutes read.

Python Logical Operators

In python, there are many operators which we use in our program. Operators are used in manipulating a particular value or operand. Logical operators are used mainly to evaluate an...

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

Syntax of Map function in Python

In this tutorial, we will understand what the map function in Python is meant. Further, we will see the syntax to be used for the same in python language. We...

3 minutes read.

Python program to find factorial of a given number

Python program to find factorial of a given number Factorial is a non-negative integer. It is the product of all positive integers from 1 to the number we are going to...

3 minutes read.

Parameter Passing in Python

In Python, when we characterize capabilities with default values for specific boundaries, it is said to have its contentions set as a possibility for the client. Clients can either pass their...

3 minutes read.

Python String decode() method

Python String decode() method The string.decode() method decodes the string using the codec registered for encoding. Default encoding is the current default string encoding. It may raise errors to set a different error handling...

1 minute read.

Python Goto Statement

We all know that Python is the most basic and widely used programming language in the world. It is also one of the world's most popular and widely used languages....

4 minutes read.

__init__ in python

Every programmer will enclose to object-oriented programming (OOP) these days. As we know, that python is the latest and most modern programming language; python supports to implementation of object-oriented philosophy....

5 minutes read.

Socket Programming in Python

Socket Programming in Python In this tutorial, we will discuss network programming using Python programming language. We will explore all basic concept of network with Python script. Network Services in Python Python has...

9 minutes read.

Spell Corrector GUI 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...

3 minutes read.

Dynamic Typing in Python

Many key factors for developing a great programming language include how it manages its memory space. This memory space largely depends on empty memory boxes called variables that one creates...

3 minutes read.

Python List reverse() method

 Python List reverse() method The list.reverse () method in Python reverses the elements of the list in place. Syntax list.reverse() Parameter NA Example 1 # Python program explaining # the list.reverse() method weekList = ['Sun','Thurs','Mon','Fri', 'Tues', 'Wed'] print("Actual week list: ",weekList) #...

1 minute read.

Count Number of Keys in Dictionary Python

Dictionary is a particular data type in python. Dictionary stores unique values by taking different keys and their assigned values. Through this article, we will learn about python dictionary count,...

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.

How to run Python program in CMD

How to run python program in cmd Command Prompt provides us all together with a different approach for dealing with our programs. The programs can be executed by accessing the directories. In...

3 minutes read.

Python Program to Print Sum of all Elements in an Array

Python program to print sum of all elements in an array A set of objects stored in contiguous memory locations is referred to as an array. The concept is to keep...

2 minutes read.

How To Install Python In Ubuntu

How To Install Python In Ubuntu Ubuntu is free and open-source software and it is an essential part of the Linux distribution. It is a popular operating system developed by Canonical. If we...

3 minutes read.