set.intersection()

Python · Sets

Theory

.intersection(other) returns a new set with only the items that appear in both sets. With a = {1, 2, 3} and b = {2, 3, 4}, a.intersection(b) is {2, 3}. If the sets share nothing, the result is an empty set, written set(). The operator form is a & b, and you can intersect several sets at once with a.intersection(b, c).

Python implements it efficiently by looping over the smaller set and checking each item for membership in the larger one, so it costs roughly O(min(len(a), len(b))). As with union, the method accepts any iterable while & needs sets on both sides. intersection_update() or &= keeps only the common items in the original set. A related check, a.isdisjoint(b), returns True when there is no overlap at all, without building a result set.

Why we use it

Finding overlap is a frequent analysis question: customers who bought in both months, skills shared by two candidates, products stocked in every store. intersection() answers it in one fast call.

When to use it

Use intersection() or & to find common values between groups. Use isdisjoint() if you only need to know whether any overlap exists. For overlapping rows of tabular data with several columns, use a pandas merge with an inner join instead.

How it works

  1. Call a.intersection(b)
  2. Pick the smaller set to loop over
  3. Take its next item
  4. Is the item also in the other set? Yes: Add it to the result. No: Skip it.
  5. Return {2, 3}; a and b are unchanged

Syntax

set1.intersection(set2)

Example

a = {1,2,3}
b = {2,3,4}
print(a.intersection(b))

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic