Skip to content

write a python program to find the second smallest number in a list

Smallest number in array python

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… Read More »Find the Second Smallest Distinct Number in Python: Two Methods

Find the Second Smallest Distinct Number in Python: Two Methods