Find second Smallest or Largest number in a List in Python

Borislav Hadzhiev

Last updated: Apr 10, 2024

Reading timeยท6 min

- Find the second smallest number in a List in Python
- Find the second largest number in a List in Python

**To find the second smallest number in a list:**

- Use the
`set()`

class to convert the list to a`set`

. - Use the
`sorted()`

function to get a sorted list. - 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`

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.

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.

This is a three-step process:

- Use the
`list.copy()`

method to create a copy of the list. - Use the
`list.remove()`

method to remove the min value from the copy. - 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`

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.

This is a three-step process:

- Declare a variable for the smallest and second smallest numbers.
- Use a
`for`

loop to iterate over the list. - 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`

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:

- set the
`min2`

variable to the current value of`min1`

- 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.

**To find the second largest number in a list:**

- Use the
`set()`

class to convert the list to a`set`

. - Use the
`sorted()`

function to get a sorted list. - 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.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.

This is a three-step process:

- Use the
`list.copy()`

method to create a copy of the list. - Use the
`list.remove()`

method to remove the max value from the copy. - 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.

This is a three-step process:

- Declare a variable for the largest and second largest numbers.
- Use a
`for`

loop to iterate over the list. - 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:

- set the
`max2`

variable to the current value of`max1`

- 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.

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

- Find the ones, tens, hundreds and thousands digits in Python
- Find the indices of duplicate items in a List in Python
- Find Max and Min in List without max() and min() in Python
- Find Min and Max values in Tuple or List of Tuples in Python
- Find the most/least common element in a List in Python
- Find the most frequent character in a String in Python
- Find object(s) in a List of objects in Python