I am a beginner in python. This is the Nested Lists problem from HaackerRank.
Given the names and grades for each student in a Physics class of N students, store them in a nested list and print the name(s) of any student(s) having the second lowest grade.
Note: If there are multiple students with the same grade, order their names alphabetically and print each name on a new line.
I want to improve my code with better functions available in python and I want to decrease lines of code.
if __name__ == '__main__':
scorecard = []
for _ in range(int(input())):
name = input()
score = float(input())
scorecard.append([name, score])
scorecard.sort(key = lambda x:x[1])
names = []
lowest_score = scorecard[0][1]
second_lowest = 0
for i in range(len(scorecard)):
if scorecard[i][1] > lowest_score and second_lowest == 0:
second_lowest = scorecard[i][1]
if second_lowest != 0 and scorecard[i][1] == second_lowest:
names.append(scorecard[i][0])
names.sort()
for name in names:
print(name)