We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The sort() built-in function is being called with the keyword-only argument key being specified. Keyword-only just means you have to explicitly write "key=" or "reverse=" or more generally "name=" where name is the name of the keyword-only argument.
In the case of the sort() function, the key parameter accepts a function to be used to generate the values that will be compared for the sort. Here, we use lambda to quickly define a small function that says, give me x, and I will return x[1].
Since we stored the records as [score, name], this has the overall effect of extracting the name from each record to be used as the value to be compared for sorting.
That line of code is necessary due to the Output requirement of the problem, "if there are multiple students, order their names alphabetically and print each one on a new line."
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Nested Lists
You are viewing a single comment's thread. Return to all comments →
The sort() built-in function is being called with the keyword-only argument key being specified. Keyword-only just means you have to explicitly write "key=" or "reverse=" or more generally "name=" where name is the name of the keyword-only argument.
In the case of the sort() function, the key parameter accepts a function to be used to generate the values that will be compared for the sort. Here, we use lambda to quickly define a small function that says, give me
x
, and I will returnx[1]
.Since we stored the records as
[score, name]
, this has the overall effect of extracting thename
from each record to be used as the value to be compared for sorting.That line of code is necessary due to the Output requirement of the problem, "if there are multiple students, order their names alphabetically and print each one on a new line."