Python Intersection Operator for Sets
Learn the Python intersection operator (&) for sets, its syntax, behavior, and when to prefer it over the intersection() method.

The & operator is the Python intersection operator for sets. It returns a new set containing only the elements that appear in both operands. For example, {1, 2, 3} & {2, 3, 4} evaluates to {2, 3}. This operator is concise, readable, and directly expresses the set operation without requiring a method call.
The Python Intersection Operator Syntax
The intersection operator is a binary operator that works on two set operands. For the built-in set and frozenset types, the general syntax is:
set1 & set2Both set1 and set2 must be instances of set or frozenset. The result is a new set of the same type as the left operand. If the left operand is a frozenset, the result is a frozenset; otherwise, it is a regular set.
a = {1, 2, 3}
b = {2, 3, 4}
print(a & b) # {2, 3}The operation is commutative with respect to the elements: a & b contains the same elements as b & a. It is also associative with respect to the elements, so a & b & c contains the same elements as (a & b) & c. Because the left operand determines the result type, expressions that mix set and frozenset can return different types depending on operand order.
How the Intersection Operator Works on Sets
The operator returns a new set with the elements common to both operands. For hash-based sets, the average-case time complexity is O(min(len(a), len(b))); implementations typically achieve this by iterating over the smaller set and checking membership in the other. The result contains only elements present in both sets, with no duplicates.
If one of the operands is empty, the result is an empty set. If both sets share no elements, the result is also an empty set.
empty = set()
print({1, 2} & empty) # set()
print({1, 2} & {3, 4}) # set()Using the Operator vs. the intersection() Method
Python also provides the intersection() method on sets. The operator and the method behave similarly, but there is a key difference: the method can accept one or more iterables as arguments, while the operator requires both sides to be sets.
a = {1, 2, 3}
b = [2, 3, 4] # list, not a set
# This works:
print(a.intersection(b)) # {2, 3}
# This raises TypeError:
print(a & b) # TypeError: unsupported operand type(s) for &: 'set' and 'list'When you have two sets, the operator is more readable and often preferred. When you need to intersect a set with a list, tuple, or other iterable, the method is necessary. You can also convert the iterable to a set first, but that adds overhead.
| Operation | Syntax | Accepts iterables? | Returns new set? |
|---|---|---|---|
& operator | a & b | No, both must be sets | Yes |
intersection() method | a.intersection(b) | Yes | Yes |
Intersecting More Than Two Sets
The intersection operator can be chained to intersect multiple sets in a single expression. Because the operation is associative, the grouping does not affect the elements in the result.
a = {1, 2, 3, 4}
b = {2, 3, 4, 5}
c = {3, 4, 5, 6}
result = a & b & c
print(result) # {3, 4}Chaining a & b & c produces the same set as a.intersection(b, c). The chained operator is often more readable for a fixed number of sets, while the method can accept a variable number of iterables.
What Happens When Sets Do Not Overlap
When two sets have no common elements, the intersection operator returns an empty set. This is a common edge case that can affect logic that expects at least one element. For example, if you are checking whether two groups share members, an empty result is a valid outcome.
admins = {'alice', 'bob'}
moderators = {'carol', 'dave'}
shared = admins & moderators
print(shared) # set()You can use the truthiness of the result to branch: if shared: will be False for an empty set. This is idiomatic and avoids an explicit length check.
Performance and Memory Behavior of Set Intersection
The intersection operator creates a new set containing the result. It does not modify the original sets. This means the memory footprint is proportional to the size of the result, not the input sets. The average-case time complexity is the same as discussed above: O(min(n, m)).
If you are performing many intersections on large sets, allocating a new set each time can add up. If you no longer need the original set and want to update it in place, intersection_update() avoids that allocation.
a = {1, 2, 3}
b = {2, 3, 4}
a.intersection_update(b) # a becomes {2, 3}Note that intersection_update() returns None and modifies the set in place. The & operator always returns a new set.
Common Mistakes When Using the Intersection Operator
One common mistake is using & on lists or tuples. Since & is not defined for those types, you will get a TypeError. To intersect lists regardless of order, convert them to sets first and then convert back; the resulting list order is not guaranteed. If you need to preserve the original order, use a list comprehension instead.
list1 = [1, 2, 3]
list2 = [2, 3, 4]
common = list(set(list1) & set(list2))
print(common) # Contains 2 and 3, but order is not guaranteedFor order-sensitive cases:
list1 = [3, 1, 2]
list2 = [2, 3, 4]
common = [x for x in list1 if x in list2]
print(common) # [3, 2]Another mistake is assuming the operator modifies the original set. It does not. If you need the original set to be updated, use intersection_update().
Finally, remember that the operator works on frozenset as well. If you have a frozenset and a regular set, the result is a frozenset if the left operand is a frozenset. This can affect type expectations in code that later tries to modify the result.
fs = frozenset([1, 2, 3])
s = {2, 3, 4}
result = fs & s
print(type(result)) # <class 'frozenset'>When to Use the Operator vs. Other Set Operations
The intersection operator is the clearest way to express the common operation of finding common elements between two sets. Use it when both operands are sets or frozensets and you want a new set as a result. If you need to accept any iterable, use intersection(). If you need to modify an existing set, use intersection_update().
For simple, one-off intersections, the operator is the most readable choice. For code that must be generic over iterable types, the method is more flexible. The operator also works well in chained expressions where multiple sets are intersected in a single line.
Understanding these distinctions helps you write code that is both efficient and clear, and avoids the type errors that come from mixing set and non-set operands.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.