English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Funções do Python

Tipos de Dados do Python

Python File Operations

Python Objects and Classes

Python Data and Time

Python advanced knowledge

Python reference manual

Python set intersection_update() usage and example

Métodos de Conjunto do Python

intersection_update() uses the intersection of sets to update the set that calls intersection_update().

The intersection of two or more sets is the set of elements common to all sets.

For more information, please visitPython set Intersection.

junction_update() syntax is:

A.intersection_update(*other_sets)

intersection_update() parameters

intersection_update() allows an arbitrary number of parameters (sets).

Note: *Is not part of the syntax. It is used to indicate that this method allows an arbitrary number of parameters.

Intersection_update() return value

This method returns None (meaning, no return value). It only updates the set that calls the intersection_update() method.

Assuming,

result = A.intersection_update(B, C)

When you run the code:

  • result will be None

  • A equals the intersection of A, B, and C

  • B remains unchanged

  • C remains unchanged

Example1: How to use intersection_update()?

A = {1, 2, 3, 4}
B = {2, 3, 4, 5}
result = A.intersection_update(B)
print('result =', result)
print('A =', A)
print('B =', B)

When running this program, the output is:

result = None
A = {2, 3, 4}
B = {2, 3, 4, 5, 6}

Example2: intersection_update() with two parameters

A = {1, 2, 3, 4}
B = {2, 3, 4, 5, 6}
C = {4, 5, 6, 9, 10}
result = C.intersection_update(B, A)
print('result =', result)
print('C =', C)
print('B =', B)
print('A =', A)

When running this program, the output is:

result = None
C = {4}
B = {2, 3, 4, 5, 6}
A = {1, 2, 3, 4}

Métodos de Conjunto do Python