Python
sets
programming
coding tips
Python tutorial

How to join two sets in one line without using

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Joining two sets in Python is usually a one-liner, but the best expression depends on whether you want readability, mutability, or compatibility with more than two inputs. The core operation is set union, and Python gives several equivalent ways to express it. Understanding those options helps you pick the form that matches the surrounding code instead of memorizing only one operator.

Use union for the Clearest Intent

If the goal is a one-line expression without using the pipe operator, set.union is the most direct choice.

python
1a = {1, 2, 3}
2b = {3, 4, 5}
3
4combined = a.union(b)
5print(combined)

This returns a new set containing every distinct element that appears in either input.

One advantage of union is that it scales naturally to more than two inputs:

python
result = a.union(b, {6, 7}, [7, 8, 9])
print(result)

Unlike the pipe operator, union accepts any iterable, not only sets.

Build a Set from Chained Iterables

Another one-line pattern is to chain iterables and convert the result back into a set.

python
1from itertools import chain
2
3a = {1, 2, 3}
4b = {3, 4, 5}
5
6combined = set(chain(a, b))
7print(combined)

This is useful when you are already working with a mix of lists, tuples, and sets and want one uniform construction step.

It is usually less expressive than union, though, because the reader has to infer that the final set(...) call is what removes duplicates.

Use Set Unpacking for Compact Construction

Python also supports set unpacking, which can be elegant when the code is already using literal-style construction.

python
1a = {1, 2, 3}
2b = {3, 4, 5}
3
4combined = {*a, *b}
5print(combined)

This reads well for small local expressions and keeps the result visibly set-shaped. It is especially handy in comprehensions or configuration-style code where you want the output literal to be obvious.

Know the Difference Between New Set and In-Place Update

If you need a new set, use union or one of the literal-style patterns above. If you want to mutate an existing set, use update.

python
1a = {1, 2, 3}
2b = {3, 4, 5}
3
4a.update(b)
5print(a)

update is not interchangeable with one-line functional expressions because it mutates the left-hand set and returns None. That difference matters in pipelines and function arguments.

Join Many Sets Dynamically

When the number of sets is not fixed, set().union(*iterables) is often the cleanest form.

python
groups = [{1, 2}, {2, 3}, {4, 5}]
combined = set().union(*groups)
print(combined)

This works well when sets come from a list, generator, or configuration object. It also avoids writing nested unions or repeated unpacking.

Performance and Readability Tradeoffs

For two ordinary sets, a.union(b) and a | b are both clear and efficient. If you are avoiding |, that should usually be for readability or because one of the inputs is not actually a set.

Examples:

  • 'a.union(b) is explicit and beginner-friendly.'
  • 'set(chain(...)) is flexible but slightly more indirect.'
  • 'set().union(*groups) is ideal for dynamic inputs.'
  • '{*a, *b} is concise and readable for small local cases.'

In hot code paths, the performance differences are typically small compared with overall algorithm design. Prefer the form that makes intent obvious.

Handle Non-Set Inputs Intentionally

Many bugs happen because code assumes both inputs are sets when one is a list or tuple. union handles iterables gracefully, but literal unpacking and pipe-based operations may be less forgiving depending on input type and Python version expectations.

python
1a = {1, 2, 3}
2b = [3, 4, 5]
3
4combined = a.union(b)
5print(combined)

If the caller can provide any iterable, union is usually the safest public API choice.

Practical Helper Pattern

For reusable code, wrap the behavior in a helper that makes the contract obvious.

python
1def join_unique(left, right):
2    return set(left).union(right)
3
4
5print(join_unique([1, 2, 2], {2, 3, 4}))

This pattern is useful when inputs may already contain duplicates before becoming sets.

Common Pitfalls

  • Using update inside an expression and forgetting it returns None.
  • Choosing set(chain(...)) when union would be clearer.
  • Assuming both inputs are sets when one is another iterable type.
  • Forgetting that a new set is returned unless you explicitly mutate in place.
  • Writing overly clever one-liners where a direct union call would be easier to read.

Summary

  • 'a.union(b) is the clearest one-line alternative to the pipe operator.'
  • 'set(chain(a, b)) works when you need to combine arbitrary iterables.'
  • '{*a, *b} is compact and readable for small local expressions.'
  • 'set().union(*groups) is the best dynamic pattern for many sets.'
  • Choose between returning a new set and mutating in place deliberately.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.