Last modified: Mar 17, 2026 By Alexander Williams

Python Ordered Set: Maintain Unique Item Order

Python's built-in set is a powerful tool. It stores unique items. But it has one key limitation. It does not remember the order of insertion.

This can be a problem. You might need both uniqueness and order. An ordered set solves this. It keeps items unique and remembers their sequence.

This guide explains ordered sets in Python. You will learn what they are and why they are useful. We will explore different ways to create them.

What is an Ordered Set?

An ordered set is a collection. It has two main properties. First, all elements are unique. Second, it preserves the insertion order.

Think of a list. It remembers order but allows duplicates. A standard Python set ensures uniqueness but is unordered. An ordered set combines the best of both.

It is useful in many scenarios. You might process data where the first occurrence matters. Or you need to deduplicate a list while keeping its original sequence.

Why Python's Built-in Set is Unordered

Python's standard set is implemented for speed. It uses a hash table. This allows very fast lookups, additions, and deletions.

However, this efficiency comes at a cost. The internal order is based on the hash values. It is not related to when you added the items.

Starting with Python 3.7, dictionaries preserve insertion order. But the set type does not have this guarantee. Its order is arbitrary and can change.

Creating an Ordered Set in Python

There is no official "OrderedSet" in the standard library. But you can create one using other data structures. We will look at three common methods.

Method 1: Using a List (Simple but Inefficient)

You can mimic an ordered set with a list. Check for duplicates before appending. This is easy to understand for beginners.


# Creating an ordered set using a list
ordered_set_list = []

items_to_add = ['apple', 'banana', 'apple', 'orange', 'banana']

for item in items_to_add:
    if item not in ordered_set_list:  # Check for uniqueness
        ordered_set_list.append(item) # Preserve order

print("Ordered Set (List):", ordered_set_list)

Ordered Set (List): ['apple', 'banana', 'orange']

This works. But it is inefficient for large datasets. Checking if item not in ordered_set_list is slow. It is an O(n) operation for each addition.

Method 2: Using a Dictionary (Efficient)

Since Python 3.7, dictionaries remember order. You can use dictionary keys as your set. All keys in a dictionary are unique.


# Creating an ordered set using a dictionary
items_to_add = ['zebra', 'yak', 'zebra', 'xray', 'yak']

ordered_set_dict = {}
for item in items_to_add:
    ordered_set_dict[item] = None  # Key is the element, value is placeholder

# Extract the keys to get the ordered set
ordered_result = list(ordered_set_dict.keys())
print("Ordered Set (Dict):", ordered_result)

Ordered Set (Dict): ['zebra', 'yak', 'xray']

This is much faster. Checking if a key exists in a dictionary is very quick. It is an O(1) operation on average. This method is a good balance of simplicity and performance.

Method 3: Using collections.OrderedDict

For code that needs to work on older Python versions, use OrderedDict. It is from the collections module. It guarantees order in all Python 3 versions.


from collections import OrderedDict

# Creating an ordered set using OrderedDict
items_to_add = [1, 3, 2, 1, 4, 3]

ordered_set_odict = OrderedDict()
for item in items_to_add:
    ordered_set_odict[item] = None

ordered_result = list(ordered_set_odict.keys())
print("Ordered Set (OrderedDict):", ordered_result)

Ordered Set (OrderedDict): [1, 3, 2, 4]

This is the most robust method for compatibility. The from collections import OrderedDict line gives you a reliable tool. It works the same way as the dictionary method.

Adding and Removing Items

Once you have your ordered set, you need to manage it. The process depends on the method you chose.

For the dictionary method, adding an item is simple. Use ordered_set_dict[new_item] = None. This acts like the add method in a normal set. To learn more about adding to standard sets, see our guide on Python set insert.

To remove an item, use the pop or del statement.


# Managing an ordered set from a dict
my_ordered_set = {'cat': None, 'dog': None, 'bird': None}

# Add an item
my_ordered_set['fish'] = None
print("After adding 'fish':", list(my_ordered_set.keys()))

# Remove an item using del
del my_ordered_set['dog']
print("After removing 'dog':", list(my_ordered_set.keys()))

# Remove an item using pop (returns the key)
removed = my_ordered_set.pop('bird', None)
print(f"Removed 'bird': {removed}. Current set:", list(my_ordered_set.keys()))

After adding 'fish': ['cat', 'dog', 'bird', 'fish']
After removing 'dog': ['cat', 'bird', 'fish']
Removed 'bird': None. Current set: ['cat', 'fish']

Practical Example: Deduplicating a List in Order

A common use for an ordered set is cleaning data. You have a list with duplicates. You want to remove them but keep the first occurrence of each item.


# Deduplicating a list while preserving order
user_logins = ['alice', 'bob', 'alice', 'charlie', 'bob', 'diana', 'alice']

# Use dict comprehension for a clean one-liner
unique_logins = list({login: None for login in user_logins}.keys())

print("Original logins:", user_logins)
print("Deduplicated logins (order kept):", unique_logins)

Original logins: ['alice', 'bob', 'alice', 'charlie', 'bob', 'diana', 'alice']
Deduplicated logins (order kept): ['alice', 'bob', 'charlie', 'diana']

This is efficient and readable. The dictionary comprehension creates the ordered set in one step. Then .keys() extracts the unique items in the correct order.

For more foundational examples of using standard sets, you can review our Python sets examples.

When to Use an Ordered Set

Choose an ordered set when order and uniqueness are both required.

Use it for processing sequences where the first unique item is important. For example, parsing log files or user input streams.

Use it for caching mechanisms where you need to evict the oldest unique entry. This is known as a Least Recently Used (LRU) cache pattern.

Avoid it if you only need uniqueness and order does not matter. The built-in set is faster for pure membership tests and set operations like union.

Conclusion

Python does not have a built-in ordered set type. But you can easily create one. The best method is using a dictionary with placeholder values. It is fast and simple.

Remember the key points. A standard set is unordered. An ordered set preserves insertion order. Use a dictionary for Python 3.7+. Use OrderedDict for older versions or guaranteed compatibility.

This concept is useful in data cleaning and pipeline processing. It helps maintain the sequence of unique events or items. Mastering this technique adds a valuable tool to your Python skills. For a different kind of Python setup, like for a content management system, you might explore our guide on installing Plone 6.