set.union()

Python · Sets

Theory

.union(other) returns a new set containing every item that is in either set, with each value appearing only once. With a = {1, 2, 3} and b = {2, 3, 4}, a.union(b) is {1, 2, 3, 4}. Neither a nor b is changed. The operator form a | b does the same thing, and you can combine several sets at once: a.union(b, c) or a | b | c.

There is a small difference between the two forms: the method accepts any iterable, so a.union([4, 5]) works with a list, while the | operator requires both sides to be sets. To modify a set in place instead of creating a new one, use a.update(b) or a |= b. Union costs about O(len(a) + len(b)) because every item from both sides is hashed once. A practical example is merging customer IDs from two campaigns to get everyone reached at least once.

Why we use it

Combining groups without double counting is a common need: all products sold in either city, every user active this week or last week. union() merges them and removes overlap automatically.

When to use it

Use union() or | to combine groups of unique values. Use update() or |= when you want to grow an existing set in place. If you need to keep duplicates or order, concatenate lists instead.

How it works

  1. Call a.union(b)
  2. Start a new set with a copy of a
  3. Take the next item from b
  4. Is it already in the new set? Yes: Skip it. No: Add it.
  5. Return {1, 2, 3, 4}; a and b are unchanged

Syntax

set1.union(set2)

Example

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