Difference() of Sets in Python

Difference() of Sets in Python

The difference() method returns the difference between the sets. It returns the element which is present in the first set but not present in other sets.

‘-‘ symbol denotes difference of sets.

difference_update of sets in python

Difference() of Sets

In Python, you can find the difference between sets using the difference() method or the - operator. The difference between two sets contains the elements that are present in the first set but not in the second set.

Let’s understand the difference() method using an example:

set 1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
set 2 = { 2, 4, 6, 8, 10, 12, 14, 16, 18 }

Elements which is present in set1 but not present in set2
set 1  –  set 2 = { 1, 3, 5, 7, 9 } 

Elements which is present in set2 but not present in set1
set 2  –  set 1 = { 12, 14, 16, 18 }
Python Set Difference()

Syntax :


#set1 – set2
set1.difference(set2)

#set2 – set1
set2.difference(set1)


Return Type :

The difference method returns a new set having all the elements of set1 which is not present in set2.

If the argument is not passed to the intersection(), it returns a shallow copy of the set.

Using the & operator

Run
#difference of sets using - operator

#example 1
set1 = {'P', 'Y', 'T', 'H', 'O', 'N' }
set2 = {'C', 'O', 'D', 'I', 'N', 'G' }
print( set1 - set2 )

#example 2
A = { 2, 4, 6, 8, 10, 11, 12 }
B = { 3, 6, 9, 12, 15 }
C = { 4, 8, 12, 16 }
print( A - B - C )
Output :

{'Y', 'P', 'T', 'H'}
{11, 2, 10}

Using intersection() function

Run
#difference of sets using difference() method

#example 1
set1 = {'P', 'Y', 'T', 'H', 'O', 'N' }
set2 = {'C', 'O', 'D', 'I', 'N', 'G' }
print( set1.difference(set2) )

#example 2
A = { 2, 4, 6, 8 , 10, 12, 14, 16, 18, 20 }
B = { 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 }
C = { 4, 8, 12, 16, 20, 24, 28, 32, 36, 40 }
print( A.difference(B,C) )
Output :

{'Y', 'P', 'T', 'H'}
{11, 2, 10}

Difference of three sets

A , B, C are three sets having some elements.

Approach:
  • First Calculate Difference of first two sets (A – B) and store it in any set say X.
  • Then find difference of X with the third set i.e. (X – C).

A – B – C  =  (A – B) – C  =  A – (B – C)

Run
#difference of three sets

A = { 2, 4, 6, 8 , 10, 12, 14, 16, 18, 20 }
B = { 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 }
C = { 4, 8, 12, 16, 20, 24, 28, 32, 36, 40 }

#A - B
x=A.difference(B)
print("A - B : ",x)

#( A - B ) - C = A - B - C
print("(A - B) - C  : ",x.difference(C))

#A & B & C
print( "A - B - C    : ",A.difference(B,C) )
Output :

A - B :  {2, 4, 8, 10, 14, 16, 20}
(A - B) - C    :  {2, 10, 14}
A - B - C      :  {2, 10, 14}

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription