4

I was check a solution on hacker rank where i was solving a question asking to print the name of the person with the second highest score from an input which has to be converted to a nested list first .

I understood all the logic in the code and most part of the code but why the Underscore ( _ ) in the for loop .Please explain me the code if there is a different concept .

marksheet = []
for _ in range(0,int(input())):
    marksheet.append([input(), float(input())])

second_highest = sorted(list(set([marks for name, marks in marksheet])))[1]
print('\n'.join([a for a,b in sorted(marksheet) if b == second_highest]))
Batman
  • 47
  • 1
  • 6
  • 1
    tried googling your title and got this result in the first page [Trying to understand Python loop using underscore and input](https://stackoverflow.com/q/39188827/995714) – phuclv Jul 28 '18 at 10:25

1 Answers1

6

It's a Pythonic convention to use underscore as a variable name when the returning value from a function, a generator or a tuple is meant to be discarded.

In your example, the code inside the for loop does not make any use of the values generated by range(0,int(input())), so using an underscore makes sense as it makes it obvious that the loop does not intend to make use of it.

blhsing
  • 91,368
  • 6
  • 71
  • 106