Find second Smallest or Largest number in a List in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
6 min

banner

# Table of Contents

  1. Find the second smallest number in a List in Python
  2. Find the second largest number in a List in Python

# Find the second smallest number in a List in Python

To find the second smallest number in a list:

  1. Use the set() class to convert the list to a set.
  2. Use the sorted() function to get a sorted list.
  3. Access the list item at index 1 to get the second smallest number.
main.py
def second_smallest(l): return sorted(set(l))[1] print(second_smallest([1, 1, 3, 5, 7])) # ๐Ÿ‘‰๏ธ 3 print(second_smallest([1, 3, 5, 7])) # ๐Ÿ‘‰๏ธ 3

find second smallest number in list

The code for this article is available on GitHub

If you need to find the second largest number in a list, click on the following subheading:

We used the set() class to convert the list to a set to remove any duplicates.

The sorted function returns a new sorted list from the items in the iterable.

The last step is to access the list element at index 1.

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

If you consider 1 to be the second smallest number in the list, use the next code snippet.

# Find the second smallest number in a List using heapq.nsmallest()

Alternatively, you can use the heapq.nsmallest() method.

The heapq.nsmallest() method returns a list with the N smallest elements from the provided iterable.

main.py
import heapq second_smallest = heapq.nsmallest(2, [1, 1, 3, 5, 7])[1] print(second_smallest) # ๐Ÿ‘‰๏ธ 1 second_smallest = heapq.nsmallest(2, [1, 3, 5, 7])[1] print(second_smallest) # ๐Ÿ‘‰๏ธ 3
The code for this article is available on GitHub

The heapq.nsmallest() method returns a list with the N smallest elements from the provided iterable.

We used the method to get the 2 smallest numbers in the list and accessed the list element at index 1 to get the second smallest number.

main.py
import heapq print(heapq.nsmallest(2, [1, 3, 5, 7])) # ๐Ÿ‘‰๏ธ [1, 3] print(heapq.nsmallest(2, [1, 3, 5, 7])[1]) # ๐Ÿ‘‰๏ธ 3

Alternatively, you can use the list.remove() method.

# Find the second smallest number in a List using list.remove()

This is a three-step process:

  1. Use the list.copy() method to create a copy of the list.
  2. Use the list.remove() method to remove the min value from the copy.
  3. Use the min() function to get the second smallest value.
main.py
def second_smallest(l): list_copy = l.copy() min_value = min(list_copy) list_copy.remove(min_value) return min(list_copy) print(second_smallest([1, 1, 3, 5, 7])) # ๐Ÿ‘‰๏ธ 1 print(second_smallest([1, 3, 5, 7])) # ๐Ÿ‘‰๏ธ 3

find second smallest number in list using list remove

The code for this article is available on GitHub

We used the list.copy() method to create a copy of the list.

The min() function returns the smallest item in an iterable or the smallest of two or more arguments.

main.py
my_list = [10, 5, 20] result = min(my_list) print(result) # ๐Ÿ‘‰๏ธ 5

We then used the list.remove() method to remove the min value from the copy of the list.

The list.remove() method removes the first item from the list whose value is equal to the passed-in argument.

The last step is to pass the copy of the list to the min() function to get the second smallest number.

Alternatively, you can use a for loop.

# Find the second smallest number in a List using a for loop

This is a three-step process:

  1. Declare a variable for the smallest and second smallest numbers.
  2. Use a for loop to iterate over the list.
  3. Use comparison operators to find the second smallest number.
main.py
def second_smallest(l): min1 = min2 = float('inf') for number in l: if number < min1: min2 = min1 min1 = number elif number < min2 and number != min1: min2 = number return min2 print(second_smallest([1, 1, 3, 5, 7])) # ๐Ÿ‘‰๏ธ 3 print(second_smallest([1, 3, 5, 7])) # ๐Ÿ‘‰๏ธ 3

find second smallest number in list using for loop

The code for this article is available on GitHub

We used a for loop to iterate over the list of numbers.

The min1 variable represents the smallest number in the list and the min2 variable represents the second smallest number.

On each iteration, we check if the current number is less than the min1 variable.

If the condition is met, we:

  1. set the min2 variable to the current value of min1
  2. set min1 to the number of the current iteration

Our elif statement checks if the current number is less than min2 and is not equal to min1.

If the number is greater than min1 and less than min2, then we assign the current number to the min2 variable.

After the last iteration, the min2 variable stores the second smallest number in the list.

# Find the second largest number in a List in Python

To find the second largest number in a list:

  1. Use the set() class to convert the list to a set.
  2. Use the sorted() function to get a sorted list.
  3. Access the list item at index -2 to get the second largest number.
main.py
def second_largest(l): return sorted(set(l))[-2] my_list = [2, 4, 6, 8, 8] print(second_largest(my_list)) # ๐Ÿ‘‰๏ธ 6
The code for this article is available on GitHub

We used the set() class to convert the list to a set to remove any duplicates.

The sorted() function returns a new sorted list from the items in the iterable.

The last step is to access the list element at index -2.

Negative indices can be used to count backward, e.g. my_list[-1] returns the last item in the list and my_list[-2] returns the second to last item.

# Find the second largest number in a List using heapq.nlargest()

If you consider 8 to be the second largest number in the list, use the next code snippet.

The heapq.nlargest() method returns a list with the N largest elements from the provided iterable.

main.py
import heapq second_largest = heapq.nlargest(2, [2, 4, 6, 8, 8])[1] print(second_largest) # ๐Ÿ‘‰๏ธ 8 second_largest = heapq.nlargest(2, [2, 4, 6, 8])[1] print(second_largest) # ๐Ÿ‘‰๏ธ 6
The code for this article is available on GitHub

The heapq.nlargest() method returns a list with the N largest elements from the provided iterable.

We used the method to get the 2 largest numbers in the list and accessed the list element at index 1 to get the second largest number.

main.py
import heapq print(heapq.nlargest(2, [2, 4, 6, 8])) # ๐Ÿ‘‰๏ธ [8, 6] print(heapq.nlargest(2, [2, 4, 6, 8])[1]) # ๐Ÿ‘‰๏ธ 6
Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

Alternatively, you can use the list.remove() method.

# Find the second largest number in a List using list.remove()

This is a three-step process:

  1. Use the list.copy() method to create a copy of the list.
  2. Use the list.remove() method to remove the max value from the copy.
  3. Use the max() function to get the second max value.
main.py
def second_largest(l): list_copy = l.copy() max_value = max(list_copy) list_copy.remove(max_value) return max(list_copy) print(second_largest([2, 4, 6, 8])) # ๐Ÿ‘‰๏ธ 6 print(second_largest([2, 4, 6, 8, 8])) # ๐Ÿ‘‰๏ธ 8
The code for this article is available on GitHub

We used the list.copy() method to create a copy of the list.

The max() function returns the largest item in an iterable or the largest of two or more arguments.

main.py
my_list = [15, 45, 30] result = max(my_list) print(result) # ๐Ÿ‘‰๏ธ 45

We then used the list.remove() method to remove the max value from the copy of the list.

The list.remove() method removes the first item from the list whose value is equal to the passed-in argument.

The last step is to pass the copy of the list to the max() function to get the second largest number.

Alternatively, you can use a for loop.

# Find the second largest number in a List using a for loop

This is a three-step process:

  1. Declare a variable for the largest and second largest numbers.
  2. Use a for loop to iterate over the list.
  3. Use comparison operators to find the second largest number.
main.py
def second_largest(l): max1 = max2 = float('-inf') for number in l: if number > max1: max2 = max1 max1 = number elif number > max2 and number != max1: max2 = number return max2 print(second_largest([2, 4, 6, 8])) # ๐Ÿ‘‰๏ธ 6 print(second_largest([2, 4, 6, 8, 8])) # ๐Ÿ‘‰๏ธ 6
The code for this article is available on GitHub

We used a for loop to iterate over the list of numbers.

The max1 variable represents the largest number in the list and the max2 variable represents the second largest number.

On each iteration, we check if the current number is greater than the max1 variable.

If the condition is met, we:

  1. set the max2 variable to the current value of max1
  2. set max1 to the number of the current iteration

Our elif statement checks if the current number is greater than max2 and is not equal to max1.

If the number is less than max1 and greater than max2, then we assign the current number to the max2 variable.

After the last iteration, the max2 variable stores the second largest number in the list.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev