In Python, we can find the second smallest value in a list easily using a simple function and a for loop.
list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
def findSecondSmallest(lst):
firstSmallest = min(lst[0],lst[1])
secondSmallest = max(lst[0],lst[1])
for i in range(2,len(lst)):
if lst[i] < firstSmallest:
secondSmallest = firstSmallest
firstSmallest = lst[i]
elif lst[i] < secondSmallest and firstSmallest < lst[i]:
secondSmallest = lst[i]
return secondSmallest
print(findSecondSmallest(list_of_numbers))
#Output:
1
We can also use the sort() function and get the second element of the list after sorting.
list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
def findSecondSmallest(lst):
temp_lst = lst.copy()
temp_lst.sort()
return temp_lst[1]
print(findSecondSmallest(list_of_numbers))
#Output:
1
You can also use the remove() function to remove the minimum value found by the min() function, and then use the min() function again.
list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
def findSecondSmallest(lst):
temp_lst = lst.copy()
firstSmallest = min(temp_lst)
temp_lst.remove(firstSmallest)
return max(temp_lst)
print(findSecondSmallest(list_of_numbers))
#Output:
1
There are many powerful built-in functions in the Python language which allow us to perform basic or complex tasks easily.
One such task is to find the minimum of a list of numbers.
However, what if you want to find the second smallest number in a list?
We can easily find the second smallest value in a list using a loop in Python.
To find the second smallest value in a list, we just need to keep track of the smallest number and the second smallest number, and loop over the list of numbers.
Below is an example of a Python function which gets the second smallest value in a list of numbers.
list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
def findSecondSmallest(lst):
firstSmallest = min(lst[0],lst[1])
secondSmallest = max(lst[0],lst[1])
for i in range(2,len(lst)):
if lst[i] < firstSmallest:
secondSmallest = firstSmallest
firstSmallest = lst[i]
elif lst[i] < secondSmallest and firstSmallest < lst[i]:
secondSmallest = lst[i]
return secondSmallest
print(findSecondSmallest(list_of_numbers))
#Output:
1
Using sort() to Find Second Smallest Value in List with Python
Another way that we can find the second smallest number in a list of numbers is with sorting.
We can sort a list in ascending order, and we then know that the smallest number is the first number of the sorted list, the second smallest number is the second number, and so on.
We can access the second element of a list by accessing the '1' position.
Below is an example in Python of how to get the second smallest value in a list using sort().
list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
def findSecondSmallest(lst):
temp_lst = lst.copy()
temp_lst.sort()
return temp_lst[1]
print(findSecondSmallest(list_of_numbers))
#Output:
1
Using max() and remove() to Get Second Smallest Value of List in Python
One final way you can get the second smallest value of a list in Python is to remove the minimum value from the list, and then find the minimum of the modified list.
First, we need to use the min() function to get the minimum value of the list. Then we use the list remove() function to remove the minimum value. We can then use the min() function again to get the second smallest number.
Below is an example in Python of how to get the second smallest value of a list using min() and remove().
list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
def findSecondSmallest(lst):
temp_lst = lst.copy()
firstSmallest = min(temp_lst)
temp_lst.remove(firstSmallest)
return max(temp_lst)
print(findSecondSmallest(list_of_numbers))
#Output:
1
How to Find Second Largest Value in a List Using Python
We can adjust the function which finds the second smallest number in a list to find the second largest number in a list.
To find the second largest number in a list, we just need to keep track of the largest and second largest numbers, and loop over the list of numbers.
Below is an example of a Python function which gets the second largest number in a list of numbers.
list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
def findSecondLargest(lst):
firstLargest = max(lst[0],lst[1])
secondLargest = min(lst[0],lst[1])
for i in range(2,len(lst)):
if lst[i] > firstLargest:
secondLargest = firstLargest
firstLargest =lst[i]
elif lst[i] > secondLargest and firstLargest > lst[i]:
secondLargest = lst[i]
return secondLargest
print(findSecondLargest(list_of_numbers))
#Output:
8
Hopefully this article has been useful for you to learn how to get the second smallest value in a list using Python.