Intersection_update method in set - Python

• This method modifies or updates a set by intersection with another set.
• This method can take zero to any number of arguments.
• It doesn’t return anything.
• We can pass any iterable or collection data type as an argument e.g. list, tuple, string, dictionary, set.
• When we call this method with a set(main set) and pass one iterable(another set) as an argument it iterates over the element of the main set and checks if the element is present in another set if the element is present in the set(another set) then it keeps the element otherwise it removes that element from the main set.
• This method is useful for large sets because it doesn’t create new sets which saves the memory.
• When we call this method with zero argument then it returns the shallow copy of the original set.
• If there is no common element among sets it return the empty set.

Syntax –

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

Parameter –

• iterable → any number of iterable or collections e.g. list, tuple, dictionary, set.
• Return value → None

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}

• In the above example first, we create two sets main_set and other_set with the elements.
• After creating both the sets print both the sets on the output screen.
• Then we called the .intersection_update() method with the main_set and passed the other_set as an argument.
• You can see that the main_set is modified and contains only elements that is common to both sets.

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}