Skip to main content

How to Check If a Set Is Sorted When Converted to a List in Python

When working with data processing, you often need to remove duplicates from a list while also checking if the remaining unique elements are in sorted order. A set is the most efficient tool for uniqueness, but it is unordered by definition.

This means converting list -> set -> list will remove duplicates, but the order of the resulting list is technically undefined (though it may appear sorted for small integers due to implementation details).

This guide explains how to perform this conversion and verify if the resulting list is sorted.

Understanding Set Conversion (Unordered Nature)

A set in Python is an unordered collection of unique elements. When you convert a list to a set, you lose the original order. When you convert that set back to a list, the order is based on the internal hash table structure, not insertion order or value.

original_list = [3, 1, 4, 1, 5, 9, 2, 6, 5]

# Convert to set (removes duplicates)
unique_set = set(original_list)

# Convert back to list (order is arbitrary)
result_list = list(unique_set)

print(f"Original: {original_list}")
print(f"Result: {result_list}")

Output:

Original: [3, 1, 4, 1, 5, 9, 2, 6, 5]
Result: [1, 2, 3, 4, 5, 6, 9]
note

While the output above looks sorted, this is coincidentally true for small integers in CPython. It is not guaranteed behavior.

Method 1: Checking if the Result is Sorted

To verify if the list obtained from the set is sorted, you compare it against a sorted version of itself.

original_data = [9, 5, 2, 5, 9, 1]

# 1. Deduplicate
unique_list = list(set(original_data))

# 2. Check if sorted
is_sorted = unique_list == sorted(unique_list)

print(f"Unique List: {unique_list}")
print(f"Is it sorted? {is_sorted}")

Output:

Unique List: [9, 2, 5, 1]
Is it sorted? False
warning

Do not rely on list(set(data)) returning a sorted list. If you need it sorted, sort it explicitly (see Method 2).

Method 2: Enforcing Sorting on the Result

Instead of hoping the set conversion results in a sorted list, you should enforce it explicitly using sorted(). This is cleaner and guarantees the order.

original_data = [3, 1, 4, 1, 5]

# ✅ Correct: Convert to set, then sort immediately
sorted_unique_list = sorted(set(original_data))

print(f"Sorted Unique List: {sorted_unique_list}")

Output:

Sorted Unique List: [1, 3, 4, 5]

Checking Against Original Order

Sometimes you want to check if the original list (after removing duplicates) was already sorted.

data = [1, 2, 3, 3, 4, 5] # Sorted with duplicates

# 1. Get unique elements
unique_list = list(set(data))

# 2. Check if the unique version is sorted
# This effectively checks if the hash order coincidentally matches numerical order
is_result_sorted = unique_list == sorted(unique_list)

print(f"Data: {data}")
print(f"Result List: {unique_list}")
print(f"Is Result Sorted? {is_result_sorted}")

Output:

Data: [1, 2, 3, 3, 4, 5]
Result List: [1, 2, 3, 4, 5]
Is Result Sorted? True

Conclusion

  1. Sets are Unordered: Do not assume list(set(x)) preserves order or sorts data.
  2. Verify Sorting: Use my_list == sorted(my_list) to check if any list is sorted.
  3. Enforce Sorting: Use sorted(set(x)) if your goal is to get a sorted, unique list.