set.difference()

Python · Sets

Theory

.difference(other) returns a new set with the items that are in the first set but not in the second. With a = {1, 2, 3} and b = {2, 3, 4}, a.difference(b) is {1}. Order matters here, unlike union and intersection: b.difference(a) is {4}. The operator form is a - b. Neither set is modified.

A related operation, symmetric_difference() or a ^ b, returns items in exactly one of the two sets, {1, 4} here. To remove items from a set in place, use difference_update() or a -= b. Difference runs in about O(len(a)), because each item of a is checked against b by hash. A typical use is comparing two snapshots: new_ids = today - yesterday finds IDs that appeared today, and yesterday - today finds the ones that disappeared.

Why we use it

Comparing two groups to see what is missing or new is a daily task in data validation: records in the source but not the target, customers who did not return, products no longer stocked. difference() gives the answer directly.

When to use it

Use difference() or - for 'in this group but not that one' questions, and swap the order to look the other way. Use ^ for values in only one of the two. For multi-column rows, use a pandas anti-join (merge with indicator=True) instead.

How it works

  1. Call a.difference(b)
  2. Start an empty result set
  3. Take the next item from a
  4. Is the item in b? Yes: Skip it. No: Add it to the result.
  5. Return {1}; note b.difference(a) would be {4}

Syntax

set1.difference(set2)

Example

a = {1,2,3}
b = {2,3,4}
print(a.difference(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