Quick answer: For the second-smallest distinct value in a Python iterable, use sorted(set(numbers))[1] only after confirming that there are at least two unique values. For example, [5, 2, 4, 9, 7, 2] returns 4, not 2. Below are a simple sorting solution and a one-pass solution, with tests and complexity trade-offs.
Method 1: sorted(set(numbers)) — simple and readable
Convert the input to a set to remove duplicates, sort it, then select index 1. Checking len(numbers) alone is insufficient: [2, 2] has two items but only one distinct value.
def second_smallest_sorted(numbers):
distinct = sorted(set(numbers))
if len(distinct) < 2:
raise ValueError("At least two distinct values are required")
return distinct[1]
print(second_smallest_sorted([5, 2, 4, 9, 7, 2])) # 4
For n input elements and k distinct elements, constructing the set takes expected O(n) time, and sorting takes O(k log k). Additional memory is O(k). This solution requires hashable, mutually comparable values.
Method 2: one pass — O(n) time and O(1) extra space
Track the smallest and second-smallest distinct values as the iterable is scanned. Unlike the sorting method, this accepts a generator and does not materialize the input. It assumes ordinary comparable real numbers (for example, integers and finite floats), not NaN values or mixed strings and numbers.
def second_smallest_one_pass(numbers):
first = second = None
for value in numbers:
if first is None or value < first:
second, first = first, value
elif value != first and (second is None or value < second):
second = value
if second is None:
raise ValueError("At least two distinct values are required")
return second
print(second_smallest_one_pass([12, 13, 1, 10, 34, 1])) # 10
print(second_smallest_one_pass(x for x in [2, 2, 3])) # 3
The one-pass algorithm performs O(n) comparisons and stores only two tracked values: O(1) auxiliary space. It neither sorts nor modifies the original list. The None sentinel is appropriate here because the input is restricted to numeric values; if None is a legitimate input value, use a different sentinel and define an ordering.
Test edge cases before using either method
cases = [
([5, 2, 4, 9, 7, 2], 4),
([2, 2, 3], 3),
([-8, -3, -8, 0], -3),
([9, 1], 9),
]
for values, expected in cases:
assert second_smallest_sorted(values) == expected
assert second_smallest_one_pass(values) == expected
for values in ([], [1], [2, 2]):
for function in (second_smallest_sorted, second_smallest_one_pass):
try:
function(values)
except ValueError:
pass
else:
raise AssertionError("Expected ValueError")
print("All tests passed")
The second element of a sorted list is not necessarily the second-smallest distinct value: sorting [2, 2, 3] yields [2, 2, 3], whose element at index 1 is still 2. Both functions here deliberately return 3.
Common errors and troubleshooting
IndexError: Accessing sorted(set(numbers))[1] without checking the distinct count fails for empty or all-equal input. TypeError: Mixed incomparable types such as 2 and '3' cannot be ordered directly. NaN: IEEE floating-point NaN is not ordered normally; filter or reject it before using either method. If you need the two smallest values including duplicates, this is a different problem.
Frequently asked questions
Which approach should I use? Use sorting for short, ordinary lists where readability matters. Use the one-pass approach for very large streams or generators, or when you want constant auxiliary space.
What about negative numbers? Both methods work for comparable negative integers and finite floats, as the tests demonstrate.
Where can I learn more? See the official Python sorted() documentation, set types, and PickupBrain’s Python list slicing guide.

C P Gupta is a YouTuber and Blogger. He is expert in Microsoft Word, Excel and PowerPoint. His YouTube channel @pickupbrain is very popular and has crossed 9.9 Million Views.
