Python

Difference_update method in set

Click for All Topics

Difference_update method in set - Python

  • As the name suggest that this method find difference between two sets and then update the set.
  • when we call this method with one set(main set) and pass other set as argument then it iterates over the element of the main set and checks wether the element is present in other set if element is present in other set then it removes the element from the main set, if element is not present in another set then it ignores the element and moves to the next element.
  • It can take zero to any number of arguments.
  • We can pass any iterable or collection type of data as an argument e.g. list, tuple, dictionary, set, string.
  • This method doesn’t return anything.
  • When we call this method with zero argument it returns a shallow copy of the calling set.

 

Syntax –

set_name.difference_update(iterable1, iterable2, iterable3...)

Parameter –

  • Iterable → any number of iterable or collection data type
  • Return value → None

 

Example –

				
					s1={11,12,13,14}
s2={15,13,16,17}
print("Main set:",s1)
print("Other set:",s2)
s1.difference_update(s2)
print("Updated main set:",s1)

# Output -
# Main set: {11, 12, 13, 14}
# Other set: {16, 17, 13, 15}
# Updated main set: {11, 12, 14}
				
			
  • In the above example first , we create two sets s1 and s2 with some elements.
  • After creating both the sets print both the set on the output screen.
  • Then call the difference_update method with set s1 and passed set s2 as a argument.
  • Finally print the modified main set on the output screen.

Difference update with multiple sets –

Example –

				
					main_set={'A','B','C','D','E'}
other_set1={'B','F','H','N','V'}
other_set2={'J','K','L','M','S'}

print("Main set:",main_set)
print("Other set 1:",other_set1)
print("Other set 2:",other_set2)

main_set.difference_update(other_set1,other_set2)

print("Modified set:",main_set)


# Output -
# Main set: {'C', 'A', 'D', 'E', 'B'}
# Other set 1: {'F', 'H', 'V', 'N', 'B'}
# Other set 2: {'S', 'K', 'M', 'L', 'J'}
# Modified set: {'C', 'A', 'D', 'E'}
				
			

Difference_update with other iterable or collection –

Example –

				
					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'

print('set:',my_set)
print('list:',my_list)
print('tuple:',my_tuple)
print('dictionary:',my_dict)
print('string:',my_string)

my_set.difference_update(my_list,my_tuple,my_dict,my_string)
print('modified set:',my_set)

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

Difference update without using .difference_update() method –

Example –

				
					main_set={'hello','this','is','edSlash'}
other_set={'hello','Anurag','Dubey'}

print("Main set:",main_set)
print("Other set:",other_set)

main_set=main_set-other_set
print("Modified main set:",main_set)

# Output -
# Main set: {'this', 'hello', 'edSlash', 'is'}
# Other set: {'hello', 'Anurag', 'Dubey'}
# Modified main set: {'is', 'this', 'edSlash'}