The provided answers are good, but if I could make a suggestion -- if there are ever times when the values could be repeated, such as
num_list = [2, 2, 4, 4, 6, 7, 8, 9]
...and so on, just sorting the list and getting the first index may not be what you're looking for.
By passing it through a set()
first, you'll make sure that each entry is a singleton:
def sorted_ordered_list(sequence):
return sorted(list(set(sequence)))
Then you can just index the returned list
for whichever value you're looking for, from the lowest at index 0 to the highest.
Example:
>>> my_list = [1, 5, 4, 3, 6, 3, 8, 3, 6, 7, 4, 2, 6, 7, 9, 8, 8]
>>> sorted_ordered_list(my_list)
[1, 2, 3, 4, 5, 6, 7, 8, 9] # now index the list for the desired value
>>>