You are viewing a single comment's thread. Return to all comments →
n = int(input())
students = [] grades = []
for _ in range(n): name = input() grade = float(input()) students.append((name, grade)) grades.append(grade)
sorted_grades = sorted(set(grades)) # Get unique grades and sort them second_lowest_grade = sorted_grades[1] # The second lowest grade is at index 1
second_lowest_students = [name for name, grade in students if grade == second_lowest_grade]
second_lowest_students.sort()
for student in second_lowest_students: print(student)
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Nested Lists
You are viewing a single comment's thread. Return to all comments →
Read the number of students
n = int(input())
Initialize lists to store student names and grades
students = [] grades = []
Read each student's name and grade
for _ in range(n): name = input() grade = float(input()) students.append((name, grade)) grades.append(grade)
Find the second lowest grade
sorted_grades = sorted(set(grades)) # Get unique grades and sort them second_lowest_grade = sorted_grades[1] # The second lowest grade is at index 1
Find students with the second lowest grade
second_lowest_students = [name for name, grade in students if grade == second_lowest_grade]
Sort the names alphabetically
second_lowest_students.sort()
Print each name on a new line
for student in second_lowest_students: print(student)