Python

Difference method in set

Click for All Topics

Difference method in set - Python

  • This method is used to find differences between the elements of two or more sets.
  • It returns a new set that contains unique elements of one set that is not present in other sets.
  • It can take zero to any number of arguments.
  • It can take iterable or any collection data type elements as an argument e.g. list, tuple, set, string, dictionary.
  • When we call this method with zero number of arguments then it returns the shallow copy of the set(set which is called the difference method).
  • We can also use the - operator in place of the .difference() method.
  • When we call this method with one set(main set) and pass one set(another set) as an argument then it iterates over the element of the main set and checks if the element exists in the other set, if the element does not exist then it adds the element in the new set(result set) if the element exist in another set then it ignores that element.

Syntax –

set_name.difference(iterable1,iterable2,iterable3…)

Parameters –

  • iterable → any collection or iterable elements e.g. list, tuple, dictionary, set, string.
  • Return value → returns a new set.

Example –

				
					A={1,2,3,4,5}
B={4,5,6,7,8}
s=A.difference(B)
print("Original set A:",s1)
print("Original set B:",s2)
print("Resultant set A-B :",s)

# Output -
# Original set A: {1, 2, 3, 4, 5}
# Original set B: {4, 5, 6, 7, 8}
# Resultant set A-B : {1, 2, 3}
				
			
  • In the above example first, we create two sets A and B with elements.
  • After creating both sets calculate A-B using the difference method and store the resultant set in variable s.
  • And then print all the sets on the output screen.

 

Difference method with no arguments –

Example –

				
					my_set={'this','is','edSlash'}
print("Original set:",my_set)
resultant_set=my_set.difference()
print("Resultant set:",resultant_set)

# Output -
# Original set: {'is', 'this', 'edSlash'}
# Resultant set: {'is', 'this', 'edSlash'}
				
			

Difference method with other type of collections or iterable element –

				
					my_set={'a','b','c','f','g','h'}
my_list=['a','b','c','d','e']
my_tuple=('a','b','c')
my_dict={'a':12,'b':15}
my_string='ab'
returned_set=my_set.difference(my_list,my_tuple,my_dict,my_string)
print('set:',my_set)
print('list:',my_list)
print('tuple:',my_tuple)
print('dictionary:',my_dict)
print('string:',my_string)
print('Resultant set:',returned_set)

# Output -
# set: {'a', 'f', 'g', 'b', 'h', 'c'}
# list: ['a', 'b', 'c', 'd', 'e']
# tuple: ('a', 'b', 'c')
# dictionary: {'a': 12, 'b': 15}
# string: ab
# Resultant set: {'f', 'g', 'h'}
				
			

Example –

				
					A={10,11,12,13,14,15}
B={16,17,18,19,20,21}
print("Original set A:",A)
print("Original set B:",B)
C=B.difference(A)
print("Resultant Set B-A:",C)

# Output -
# Original set A: {10, 11, 12, 13, 14, 15}
# Original set B: {16, 17, 18, 19, 20, 21}
# Resultant Set B-A: {16, 17, 18, 19, 20, 21}
				
			

Difference using - operator –

Example –

				
					set1={'a','b','c','d','e'}
set2={'b','d','e','f','g'}
resultant_set=set1-set2
print("Original set 1:",set1)
print("Original set 2:",set2)
print("Resultant set:",resultant_set)

# Output -
# Original set 1: {'e', 'c', 'd', 'a', 'b'}
# Original set 2: {'f', 'g', 'd', 'e', 'b'}
# Resultant set: {'a', 'c'}