Skip to content

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

Learn how to verify whether a Python list converted from a set maintains sorted order and how to enforce sorting when deduplicating data.

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]

unique_set = set(original_list)

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]

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]

unique_list = list(set(original_data))

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

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]

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

unique_list = list(set(data))

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