Click for All Topics
Syntax –
set_name.intersection_update(iterable1, iterable2, iterable3,...)
Parameter –
Example –
main_set={1,2,3,4,5}
other_set={2,4,6,8}
print("Main set:",main_set)
print("Other set:",other_set)
main_set.intersection_update(other_set)
print("Modified Main set:",main_set)
# Output -
# Main set: {1, 2, 3, 4, 5}
# Other set: {8, 2, 4, 6}
# Modified Main set: {2, 4}
.intersection_update()
method with the main_set and passed the other_set as an argument.Intersection_update with two arguments –
Example –
set1={'a','b','c','d','e'}
set2={'b','d','e','f','g'}
set3={'a','b','u','d','h'}
print("Set 1:",set1)
print("Set 2:",set2)
print("Set 3:",set3)
set1.intersection_update(set2,set3)
print("Modified set 1:",set1)
# Output -
# Set 1: {'e', 'd', 'c', 'b', 'a'}
# Set 2: {'e', 'd', 'g', 'f', 'b'}
# Set 3: {'d', 'u', 'h', 'b', 'a'}
# Modified set 1: {'d', 'b'}
intersection_update with zero argument –
Example –
my_set={'hello','this', 'is','edSlash'}
print("Main set:",my_set)
my_set.intersection_update()
print("Resultant set:",my_set)
# Output -
# Main set: {'is', 'this', 'edSlash', 'hello'}
# Resultant set: {'is', 'this', 'edSlash', 'hello'}
intersection_update with no common element –
Example –
s={"Hello", "Python", "set", "methods"}
s1={"Anurag", "Dubey"}
print("Main set:",s)
print("Other set:",s1)
s.intersection_update(s1)
print("Modified main set:",s)
# Output -
# Main set: {'set', 'Python', 'methods', 'Hello'}
# Other set: {'Dubey', 'Anurag'}
# Modified main set: set()
intersection_update with another type of iterable or collections –
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.intersection_update(my_list,my_tuple,my_dict,my_string)
print('Modified set:',my_set)
# Output -
# set: {'b', 'f', 'h', 'c', 'g', 'a'}
# list: ['a', 'b', 'c', 'd', 'e']
# tuple: ('a', 'b', 'c')
# dictionary: {'a': 12, 'b': 15}
# string: ab
# Modified set: {'b', 'a'}
intersection_update without using an inbuilt method –
Example –
s={10,20,30,40,50,60}
s1={10,30,50,70,90}
print("Main set:",s)
print("Other set:",s1)
s&=s1
print("Modified main set:",s)
# Output -
# Main set: {50, 20, 40, 10, 60, 30}
# Other set: {50, 90, 70, 10, 30}
# Modified main set: {50, 10, 30}
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India