Python Set intersection_update()


Python Set intersection_update() method

Python Set intersection_update() method updates the set with the result of intersection operation between the set and a given iterable.

In this tutorial, you will learn the syntax and usage of Set intersection_update() method, with examples.

Syntax of Set intersection_update()

The syntax to call intersection_update() method is

set.intersection_update(s)

Parameters

Set intersection_update() method takes one parameter.

ParameterDescription
sRequired
An iterable object. Could be a list, set, tuple, string, dictionary keys, dictionary values, etc.
Python Set intersection_update() method parameters table

Return value

intersection_update() method returns None.

Please note that the Set intersection_update() method modifies the original set.

Usage

The following statement shows how to update a set set_1 with the intersection of the set_1 and iterable s using set.intersection_update() method, and store the result in set_output.

set_1.intersection_update(s)

This statement is equivalent to the following operation.

set_1 = set_1.intersection(set_2)

Examples

1. Updating set with the intersection of itself and another set in Python

In this example, we shall take two sets in set_1 and set_2. We have to update the set set_1 with the intersection of set_1 and set_2.

Call intersection_update() method on set_1 object, and pass set_2 object as argument. After intersection_update() method executes, set_1 ends up with the intersection of set_1 and set_2.

Python Program

# Take two sets
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'a', 'k', 'd', 'm'}

print(f"set_1 before update : {set_1}")

# Intersection Update set_1
set_1.intersection_update(set_2)

print(f"set_1 after  update : {set_1}")

Output

set_1 before update : {'c', 'a', 'd', 'b'}
set_1 after  update : {'a', 'd'}

2. Updating set with the intersection of itself and a list in Python

In this example, we shall take a set in set_1 and a list in list_1. We have to update the set set_1 with the intersection of items from the set set_1 and the list list_1.

Call intersection_update() method on set_1 object, and pass list_1 object as argument. After intersection_update() method executes, set_1 ends up with the intersection of the items from set_1 and list_1.

Python Program

# Take two sets
set_1 = {'a', 'b', 'c', 'd'}
list_1 = ['a', 'o', 'm', 'c']

print(f"set_1 before update : {set_1}")

# Intersection Update set_1
set_1.intersection_update(list_1)

print(f"set_1 after  update : {set_1}")

Output

set_1 before update : {'d', 'a', 'b', 'c'}
set_1 after  update : {'a', 'c'}

3. Updating set with intersection of items from itself and a string in Python

String is a collection of characters. Characters are the items in a string.

In this example, we shall take a set in set_1 and a string in str_1. We have to update the set set_1 with the intersection of items from the set set_1 and the character from string str_1.

Call intersection_update() method on set_1 object, and pass str_1 object as argument.

Python Program

# Take two sets
set_1 = {'a', 'b', 'c', 'd'}
str_1 = 'apple'

print(f"set_1 before update : {set_1}")

# Intersection Update set_1
set_1.intersection_update(str_1)

print(f"set_1 after  update : {set_1}")

Output

set_1 before update : {'c', 'a', 'b', 'd'}
set_1 after  update : {'a'}

Summary

In this Python Set Tutorial, we learned how to use Set intersection_update() method to update a set with the items from the intersection operation of the original set and a given iterable in Python, with the help of well detailed examples.