×

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

Python if statement

Python if statement Decision making is an essential feature of any programming languages. Condition checking is the strength of decision making. Python provides many decision-making statements, such as: If statementIf-else statementelif statement if statement The if statement is...

2 minutes read.

Python Simple Interest

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

3 minutes read.

Write a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

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

Python - Binomial Distribution

Introduction Definition of the Binomial Distribution The method of counting how many instances of a specific event there have been is called the binomial distribution. It will outline the possible outcomes or...

4 minutes read.

How to Practice Python Programming

Learning python kkis a step towards coding. Python gets one closer to programming languages. It is essential to practice every programming language to become a professional in coding. It is...

4 minutes read.

Python Pass Statement

The pass statement is a null statement. The difference between pass and comment is that comment is ignored by the interpreter, whereas pass is not. The pass statement is typically used...

3 minutes read.

Python Dictionary values() method

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

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

Python List count() method

Python List count() method The list.count() method returns the number of times x appears in the list. Syntax list.count(x) Parameter x: This parameter represents the value to search for and can contain any iterable (list, set, tuple, etc.) Return This method returns the...

1 minute read.

Difference between Input() and raw_input() functions in Python

There are two input functions in Python that are used for taking input are given below: raw_input()input() What is raw_input() Function? raw_input() function is a built-in function in Python. It is used to...

6 minutes read.

Python List sort() method

The list.sort () method in Python sorts the items of the list in place. Syntax list.sort(key=None, reverse=False) Parameter reverse: If a Boolean value ‘True’ is passed, the  sorting will be done in the descending order else for ‘False’...

2 minutes read.

Python Set add() Method

Python Set add() Method The set.add() method adds the specified element to a set. If the element is already present in the set, it doesn't add it. Syntax set.add(element) Parameter element- This parameter represents the element that...

1 minute read.

Python Break Statement

In Python, loops are used to automate and repeat processes in an effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration,...

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.

Python Ways to find nth occurrence of substring in a string

Introduction In Python, a string is a collection of characters that can be employed to conduct additional operations. In Python, a substring is a group of characters that are a part...

4 minutes read.

Map Syntax in Python

Introduction: In Python, a function called map acts as an iterator, returning a result after each item in an iterable has been subjected to a function (tuple, lists, etc.). When you...

6 minutes read.

Python Variables, Constants and Literals

Python is today's most valuable, easy-to-implement and sought-out programming language. Nowadays, developers want to focus on implementing the program rather than spending time with complex programs. So, for this reason,...

17 minutes read.

Flutter with tensor flow 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...

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.