Last updated: Apr 10, 2024
Reading timeยท6 min
To find the second smallest number in a list:
set()
class to convert the list to a set
.sorted()
function to get a sorted list.1
to get the second smallest number.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
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
.
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.
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 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.
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:
list.copy()
method to create a copy of the list.list.remove()
method to remove the min value from the copy.min()
function to get the second smallest value.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
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.
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:
for
loop to iterate over the list.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
We used a for
loop to iterate over the list of numbers.
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:
min2
variable to the current value of min1
min1
to the number of the current iterationOur 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:
set()
class to convert the list to a set
.sorted()
function to get a sorted list.-2
to get the second largest number.def second_largest(l): return sorted(set(l))[-2] my_list = [2, 4, 6, 8, 8] print(second_largest(my_list)) # ๐๏ธ 6
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
.
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.
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 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.
import heapq print(heapq.nlargest(2, [2, 4, 6, 8])) # ๐๏ธ [8, 6] print(heapq.nlargest(2, [2, 4, 6, 8])[1]) # ๐๏ธ 6
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:
list.copy()
method to create a copy of the list.list.remove()
method to remove the max value from the copy.max()
function to get the second max value.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
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.
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:
for
loop to iterate over the list.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
We used a for
loop to iterate over the list of numbers.
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:
max2
variable to the current value of max1
max1
to the number of the current iterationOur 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: