I am assuming that you need to get the element which is right before the minimum of the list.
For example, [4,5,6,2,3] will get you '6', the element right before the smallest number.
To do that, you first need to find the index of the smallest element. First, you find out the smallest element, and then find it's index. In Python, you can use the min
and the index
methods. The min
method finds out the smallest element in a list, and the index
method returns the index of the number in the list.
a = [4, 5, 6, 2, 3]
smallestNum = min(a) #getting the smallest number in the list
indexOfSmallestNum = a.index(smallestNum) # Getting the index of the smallest number
# Now, if the indexOfSmallestNum is 0, there is no element before it.
#Hence, we return a None.
#Else, we return a[indexOfSmallestNum-1], the element before the smallest number.
if indexOfSmallestNum == 0:
return None
else
return a[indexOfSmallestNum-1]
OR If you just want to find out the second smallest element, then the question is a duplicate, and it can be found here.