How to find the second lowest lists into a nested list by their second value?

Solution 1:

Get the min of the total elements, filter using that valid then get min of remaining and keep elements equal to min of remaining:

from operator import itemgetter
# min of all elements
mn = min(nl, key=itemgetter(1))[1]

# remove elements equal to min
filtered = [x for x in nl if x[1] != mn]

# get min of remaining
mn_fil = min(filtered,key=itemgetter(1))[1]

# filter remaining
out = [x for x in filtered if x[1] == mn_fil]
print(out)

[['Harsh', 20], ['Beria', 20]]

Works for both your cases:

In [19]: nl = [['Prashant', 32], ['Pallavi', 36], ['Dheeraj', 39], ['Shivam', 40]]    
In [20]: from operator import itemgetter    
In [21]: mn = min(nl, key=itemgetter(1))[1]    
In [22]: filtered = [x for x in nl if x[1] != mn]    
In [23]: mn_fil = min(filtered,key=itemgetter(1))[1]    
In [24]: out = [x for x in filtered if x[1] == mn_fil]    
In [25]: out
Out[25]: [['Dheeraj', 36]]

Using a single for loop we remove all elements from the temp list if we find a lower element, if we find and equally lower one we append it:

mn = min(nl, key=itemgetter(1))[1]
temp = []
best = float("inf")
for ele in nl:
    if mn < ele[1] < best:
        best = ele[1]
        temp = []
        out.append(ele)
    elif ele[1] == best:
        temp.append(ele)
print(temp)

Solution 2:

I did it by finding the second lowest value using a set and then selecting elements from the list with the same value.

#ordering by value
nl.sort(key = lambda x: x[1])

values_set = set()
for value in nl:
    values_set.add(value[1])

values_list = list(values_set)
#ordering
values_list.sort()
#getting second lowest values
lowest_values = [lowest for lowest in nl if lowest[1] == values_list[1] ]

Solution 3:

if __name__ == '__main__':
arr = []
for _ in range(int(input())):
    name = input()
    score = float(input())
    arr1 = [name, score]
    arr.append(arr1)
arr.sort(key=lambda x: x[1])
# print(arr)
# print(min(arr,key=lambda x:x[1]))
arr.remove(min(arr,key=lambda x:x[1]))
# print(arr)
minimum = min(arr,key=lambda x:x[1])
# print(minimum[1])
a=[]
minimum = minimum[1]
for i  in arr:
    if(i[1] == minimum):
        a.append(i[0])
a.sort()
for i in a:
    print(i)