Python Forum
How to find the second lowest element in the list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find the second lowest element in the list?
#1
I have a list of students and I have to print the names of students who have scored second lowest.

students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]]

The lowest grade of belongs to Tina. The second lowest grade of belongs to both Harry and Berry, so we order their names alphabetically and print each name on a new line.

I am extremely new to python so I have coded as below -

 studList = []
for i in range(int(input())):
  name = input()
  score = float(input())
  studList.append([name,score])
  
studList = sorted(studList, key = lambda x: x[1], reverse = True)

print(studList)  
I have done till the point that I now have a sorted list but now I am not sure I do I extract the second lowest scorer's names.
Reply
#2
  • use operator.itemgetter or lambda for the key function
  • sort your list reversed (big numbers first) with the key function
  • use the index -2 to access the second-smallest element


from operator import itemgetter

values = [('A', 4), ('B', 3), ('C', 1), ('D', 6), ('E', 1), ('F', -10), ('G', -20)]
# the second smallest number is -10

name_getter = itemgetter(0)
value_getter = itemgetter(1)

# itemgetter is useful as a key function for sorting
# this prevents the use of lambda
sorted_values = sorted(values, reverse=True, key=value_getter)

# btw, If you had chosen (value, name), then the key function is not required.
# 


 # get both, name and value
result = sorted_values[-2]
print(result)

# name_getter return index 0 from result
print(name_getter(result))
# or
print(result[0])
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Hello,
>>> students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]]
>>> studList = [student[0] for student in sorted(students, key = lambda x: x[1], reverse = True)]
>>> print(studList)
['Akriti', 'Harsh', 'Harry', 'Berry', 'Tina']
>>>
édit: sorry Confused
I speak Python but I don't speak English (I just read it a little). If I express myself badly, please blame the translator^^.
Reply
#4
students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]]

sorted_students = sorted(students, key=lambda x:x[1], reverse=True)

print(f"Second lowest = {sorted_students[-2]}", end = '')
if sorted_students[-3][1] == sorted_students[-2][1]:
    print(f" and {sorted_students[-3]}")
else:
    print()
results:
Output:
Second lowest = ['Berry', 37.21] and ['Harry', 37.21]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Find (each) element from a list in a file tester_V 3 281 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 337 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  [SOLVED] [Beautifulsoup] Find if element exists, and edit/append? Winfried 2 456 Sep-03-2022, 10:14 PM
Last Post: Winfried
  read a text file, find all integers, append to list oldtrafford 12 1,071 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 498 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  find some word in text list file and a bit change to them RolanRoll 3 545 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  check if element is in a list in a dictionary value ambrozote 4 890 May-11-2022, 06:05 PM
Last Post: deanhystad
  Python Program to Find the Total Sum of a Nested List vlearner 8 1,791 Jan-23-2022, 07:20 PM
Last Post: menator01
  Find the highest value of a list Menthix 4 1,062 Oct-29-2021, 02:32 PM
Last Post: Menthix
  sorting a list of lists by an element leapcfm 3 1,101 Sep-10-2021, 03:33 PM
Last Post: leapcfm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020