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.
He has a for loop building the nested list called marksheet.
Then "marks for name, marks in marksheet" is a list comprehension. It's extracting the second item - the scores - from marksheet into a new list. He then turns the list into a set (which removes the duplicates), turns it back into a list and then sorts the list. The second item in the list is the second lowest score.
"a for a,b in sorted(marksheet) if b == second_highest" is another list comprehension. He sorts the marksheet (the list he built at the beginning, containing both names and scores); because the name is the first in each pair, this is basically putting the list in alphabetical order He's taking the first item in each pair - the name - if the second item - the score - matches the second lowest.
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 →
He has a for loop building the nested list called marksheet.
Then "marks for name, marks in marksheet" is a list comprehension. It's extracting the second item - the scores - from marksheet into a new list. He then turns the list into a set (which removes the duplicates), turns it back into a list and then sorts the list. The second item in the list is the second lowest score.
"a for a,b in sorted(marksheet) if b == second_highest" is another list comprehension. He sorts the marksheet (the list he built at the beginning, containing both names and scores); because the name is the first in each pair, this is basically putting the list in alphabetical order He's taking the first item in each pair - the name - if the second item - the score - matches the second lowest.