Does Python have an ordered set?
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
Python does not have a built-in OrderedSet type in the standard library. However, since dictionaries preserve insertion order in modern Python, you can get most ordered-set behavior by storing keys in a dict and ignoring the values.
What An Ordered Set Means
A normal set gives you uniqueness but not a guaranteed insertion order interface. An ordered set usually means:
- Elements stay unique.
- Iteration follows insertion order.
- Membership tests stay fast.
Python's regular set handles the first and third points well, but not the second in a way you should rely on for program semantics.
The Simplest Practical Replacement
For many cases, dict.fromkeys is enough:
This is the most common answer when the real goal is "remove duplicates but keep order."
If you want a reusable ordered set object, store elements as dictionary keys:
That gives you insertion order plus uniqueness with very little code.
Why A Dictionary Works
From Python 3.7 onward, insertion order is a language guarantee for dictionaries. In CPython 3.6, it was already true as an implementation detail, but 3.7 is the version where you can rely on it portably.
Because dictionary keys are unique, the structure naturally behaves like an ordered set. The values are irrelevant, so a placeholder such as None is enough.
When A Real set Is Still Better
If you only care about uniqueness and fast membership and do not need stable iteration order, use a regular set:
The standard set API is richer and communicates intent better when ordering does not matter.
Ordered Deduplication In Practice
A common use case is preserving the first occurrence of items:
This is often simpler than introducing a full ordered-set abstraction, especially in data-cleaning code.
Limits Of The dict-Based Pattern
The dict-based approach gives insertion order, but it is not a full standard-library ordered-set type with every method you might expect. If you need set algebra such as union, intersection, and difference while preserving order, you either implement those methods yourself or use a dedicated third-party package.
For example, an ordered union can be written like this:
That is easy enough for simple applications, but if ordered-set operations are central to your program, a dedicated implementation may be worth it.
Common Pitfalls
The biggest mistake is assuming set preserves insertion order just because it appears stable in one run. That behavior is not the contract you should build logic around.
Another common issue is overengineering the problem. If all you need is ordered deduplication, list(dict.fromkeys(values)) is usually enough.
Developers also sometimes forget that ordered-set elements still need to be hashable. A list or dictionary cannot become a key in the backing dictionary unless you convert it into a hashable form first.
Finally, be clear about your Python version. Relying on dictionary insertion order makes sense in modern Python, but old-version portability may require more caution.
Summary
- Python does not ship a built-in
OrderedSetin the standard library. - Modern dictionaries preserve insertion order, which makes them a practical replacement.
- '
list(dict.fromkeys(values))is the simplest way to deduplicate while keeping order.' - A small wrapper around
dictcan provide an ordered-set-like API. - Use a regular
setwhen ordering does not matter.
Related reading
- Does RabbitMQ call the callback function for a consumer when it has some message for it?
- Does rabbitmq support binding a single queue to multi exchanges?
- Does Tensorflow simplify a computational graph?
- Does using new on a struct allocate it on the heap or stack?
- Does Python have "private" variables in classes?
- Does Python optimize tail recursion?
- Does YugaByte DB’s YSQL API support array types
- Double Iteration in List Comprehension

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.