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.
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.
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:
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.
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.
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.
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.
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.
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.
This pattern is useful when inputs may already contain duplicates before becoming sets.
Common Pitfalls
- Using
updateinside an expression and forgetting it returnsNone. - Choosing
set(chain(...))whenunionwould 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
unioncall 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
- How to JSON serialize sets?
- How to keep keys/values in same order as declared?
- How to keep track of depth in breadth first search?
- How to list _all_ objects in Amazon S3 bucket?
- How to keep a Python script output window open?
- How to keep index when using pandas merge
- How to load a graph with tensorflow.so and c_api.h in c language?
- How to log formatted message, object array, exception?

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 courseTrack 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.