YouTip LogoYouTip

Ref Set Intersection_Update

Python Set intersection_update() Method | Tutorial

Tutorial -- Learning is not just about technology, but also about dreams!

Python 3 Tutorial

Python3 Tutorial Python3 Introduction Python3 Environment Setup Python3 VScode Python3 Basic Syntax Python3 Basic Data Types Python3 Data Type Conversion Python3 Interpreter Python3 Comments Python3 Operators Python3 Numbers Python3 Strings Python3 Lists Python3 Tuples Python3 Dictionaries Python3 Sets Python3 Conditional Statements Python3 Loops Python3 First Program Python3 Comprehensions Python3 Iterators & Generators Python3 with Python3 Functions Python3 Lambda (Anonymous Functions) Python Decorators Python3 Data Structures Python3 Modules Python __name__ Python3 Input & Output Python3 File Python3 OS Python3 Errors & Exceptions Python3 Object Oriented Python3 Namespace/Scope Python Virtual Environment Creation Python Type Hints Python3 Standard Library Overview Python3 Examples Python Quiz

Python3 Advanced Tutorial

Python3 Regular Expressions Python3 CGI Programming Python3 MySQL (mysql-connector) Python3 MySQL (PyMySQL) Python3 Network Programming Python3 SMTP Sending Email Python3 Multithreading Python3 XML Processing Python3 JSON Python3 Date & Time Python3 Built-in Functions Python3 MongoDB Python3 urllib Python uWSGI Installation & Configuration Python3 pip Python3 operator Module Python math Module Python requests Module Python random Module Python OpenAI Python Useful Resources Python AI Drawing Python statistics Module Python hashlib Module Python Quantitative Python pyecharts Module Python selenium Library Python Web Scraping Python Scrapy Library Python Markdown Python sys Module Python Pickle Module Python subprocess Module Python queue Module Python StringIO Module Python logging Module Python datetime Module Python re Module Python csv Module Python threading Module Python asyncio Module Python PyQt Python for Loop Python while Loop

Python3 Dictionaries Python3 Conditional Statements

Python3.x Python Set intersection_update() Method

Python Sets Python Sets


Description

The intersection_update() method is used to find the elements that are common to two or more sets, i.e., to compute the intersection.

The intersection_update() method is different from the intersection() method because the intersection() method returns a new set, while the intersection_update() method removes the non-overlapping elements from the original set.

Syntax

Syntax of the intersection_update() method:

set.intersection_update(set1, set2 ... etc)

Parameters

  • set1 -- Required, the set to find common elements with.
  • set2 -- Optional, other sets to find common elements with. Multiple sets can be provided, separated by commas (,).

Return Value

None.

Examples

Remove elements from set x that are not present in set y:

Example 1

x = {"apple", "banana", "cherry"} # y set does not contain banana and cherry, they are removed
y = {"google", "tutorial", "apple"}
x.intersection_update(y)
print(x)

Output:

{'apple'}

Compute the intersection of multiple sets:

Example 1

x = {"a", "b", "c"}
y = {"c", "d", "e"}
z = {"f", "g", "c"}
x.intersection_update(y, z)
print(x)

Output:

{'c'}

Python Sets Python Sets

← Ref Set IssubsetRef Set Discard β†’