In Python, we can find the second largest 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 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
We can also use the sort() function and get the second to last element of the list after sorting.
list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
def findSecondLargest(lst):
temp_lst = lst.copy()
temp_lst.sort()
return temp_lst[-2]
print(findSecondLargest(list_of_numbers))
#Output:
8
You can also use the remove() function to remove the maximum value found by the max() function, and then use the max() function again.
list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
def findSecondLargest(lst):
temp_lst = lst.copy()
firstLargest = max(temp_lst)
temp_lst.remove(firstLargest)
return max(temp_lst)
print(findSecondLargest(list_of_numbers))
#Output:
8
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 maximum of a list of numbers.
However, what if you want to find the second largest number in a list?
We can easily find the second largest value in a list using a loop in Python.
To find the second largest value in a list, we just need to keep track of the largest number and the second largest number, and loop over the list of numbers.
Below is an example of a Python function which gets the second largest value 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
Using sort() to Find Second Largest Value in List with Python
Another way that we can find the second largest number in a list of numbers is with sorting.
We can sort a list in ascending order, and we then know that the largest number is the last number of the sorted list, the second largest number is the second to last number, and so on.
We can access the second to last element of a list by accessing the ‘-2’ position.
Below is an example in Python of how to get the second biggest value in a list using sort().
list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
def findSecondLargest(lst):
temp_lst = lst.copy()
temp_lst.sort()
return temp_lst[-2]
print(findSecondLargest(list_of_numbers))
#Output:
8
Using max() and remove() to Get Second Largest Value of List in Python
One final way you can get the second biggest value of a list in Python is to remove the maximum value from the list, and then find the maximum of the modified list.
First, we need to use the max() function to get the maximum value of the list. Then we use the list remove() function to remove the maximum value. We can then use the max() function again to get the second biggest number.
Below is an example in Python of how to get the second largest value of a list using max() and remove().
list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
def findSecondLargest(lst):
temp_lst = lst.copy()
firstLargest = max(temp_lst)
temp_lst.remove(firstLargest)
return max(temp_lst)
print(findSecondLargest(list_of_numbers))
#Output:
8
How to Find Second Smallest Value in a List Using Python
We can adjust the function which finds the second largest number in a list to find the second smallest number in a list.
To find the second smallest number in a list, we just need to keep track of the smallest and second smallest numbers, and loop over the list of numbers.
Below is an example of a Python function which gets the second smallest number 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
Hopefully this article has been useful for you to learn how to get the second largest value in a list using Python.