Python program to display all second lowest grade student name from nested list

PythonServer Side ProgrammingProgramming

Suppose we have the names and grades for each student in a nested list we have to display the names of any students having the second lowest grade. If there are more than one students with the second lowest grade, reorder these in an alphabetical order and print each name on a new line.

So, if the input is like students = [['Amal',37],['Bimal',37],['Tarun',36],['Akash',41],['Himadri',39]], then the output will be Amal, Bimal both have second least score 37, they are placed in alphabetic order.

To solve this, we will follow these steps −

  • min_mark := minimum of scores for all x in students
  • students := a list of students x for all x in students if score of > min_mark
  • min2_mark := minimum of scores for all x in students
  • students := sort the list [with names of x for all x in students if score of x is same as
  • min2_mark]
  • for each x in students, do
    • display x

Example

Let us see the following implementation to get better understanding

def solve(students):
   min_mark = min(x[1] for x in students)
   students = [x for x in students if x[1] > min_mark]
   min2_mark = min(x[1] for x in students)
   students = sorted([x[0] for x in students if x[1] == min2_mark])
   for x in students:
      print(x)

students = [['Amal',37],['Bimal',37],['Tarun',36],['Akash',41],['Himadri',39]]
solve(students)

Input

[['Amal',37],['Bimal',37],['Tarun',36],['Akash',41],['Himadri',39]]

Output

Amal
Bimal
raja
Published on 11-Oct-2021 09:25:28
Advertisements