intersection_update(*others) removes from the set every item that is missing from at least one of the iterables you pass. What is left is the overlap. The set is changed in place.
It does the same job as intersection(), but instead of returning a new set it overwrites the original and returns None.
set.intersection_update() is an instance method of the built-in set type: call it on a set with dot notation, such as post_tags.intersection_update(...). No import is needed.
Syntax
Pass one or more iterables, and call it on its own line, because the return value is always None.
python
s.intersection_update(iterable)
s.intersection_update(iterable1, iterable2)
s &= other_set
Call
What it does
{1, 2, 3}.intersection_update({2, 3, 4})
Returns None; the set becomes {2, 3}.
{1, 2, 3}.intersection_update([3, 4], {3, 5})
Returns None; the set becomes {3}, the only item present everywhere.
{1, 2}.intersection_update({3})
Returns None; the set becomes set(), because nothing is shared.
s &= other has the same effect, but needs another set on the right. If you need to keep the original items, use intersection(), which returns a new set instead.
Method signatures
python
set.intersection_update(*others)
# returns: None; the set now holds only items present in every iterable
# raises: TypeError if an argument is not iterable or an item is unhashable
Parameters
Parameter
Description
*others
Zero or more iterables: set, list, tuple, string, range, and so on. An item stays only if it appears in all of them.
Return Value
Return Value
Description
None
Always None. The set itself is smaller, or empty (set()) when nothing is shared.
Python Examples
Example 1 — Keep Only the Approved Tags on a Post
Filter a tag set against an approved list, in place, and check what the method returns.