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.
Pythonic aimes for the ideal mix of the explicit and the expressive, so no. Your compactness is at the expense of efficiency as well as clear meaning - you sort the list, then traverse the unsorted list. You traverse the entire list twice. You do more sorting than you need, because you're sorting alphabetically the names of students whom you will discard. That can be improved significantly.
I've said elsewhere that using nested lists for this is an inappropriate solution (dicts much better). But if you want to use nested lists then
Sort the list by score only.
Drop from the front of the list all elements with the same score as the first element
take all the elements from the new sequence with the same score as the first element of the new sequence, discarding the scores and ignoring the rest of the new sequence
Sort what you have left.
If you want to be efficient and compact to the point of unreadability, here's how your code could be changed to do that...
For larger data sets with a spread of scores, that's going to be much more efficient than your solution (partly because I use generators and iterators, partly because I do as little traversal as possible). But neither of them are pythonic. Line noise is not pythonic.
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 →
Pythonic aimes for the ideal mix of the explicit and the expressive, so no. Your compactness is at the expense of efficiency as well as clear meaning - you sort the list, then traverse the unsorted list. You traverse the entire list twice. You do more sorting than you need, because you're sorting alphabetically the names of students whom you will discard. That can be improved significantly.
I've said elsewhere that using nested lists for this is an inappropriate solution (dicts much better). But if you want to use nested lists then
If you want to be efficient and compact to the point of unreadability, here's how your code could be changed to do that...
For larger data sets with a spread of scores, that's going to be much more efficient than your solution (partly because I use generators and iterators, partly because I do as little traversal as possible). But neither of them are pythonic. Line noise is not pythonic.