Click for All Topics
Syntax –
set_name.difference_update(iterable1, iterable2, iterable3...)
Parameter –
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}
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'}
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India