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.
list a is simply a list of all the input data. It is sorted so that the lowest score is in the first index.
list b uses a list comprehension to remove all instances of the lowest score from list a. The lowest remaining score in the resulting list (found in the first index since it will be the first value taken from the already-sorted list a) is the second-lowest score overall.
list c then uses another list comprehension to trim down list b to only those scores that are equal to that second-lowest overall score.
list c is now a nested list containing the scores and names of the students that will make up the output.
It is sorted alphabetically according to student name by using a lambda expression which indicates to use the contents of the second index of each object to be sorted ('name' in [score, name]) as the key for sorting.
Finally the names from list c are printed to satisfy the problem.
Nested Lists
You are viewing a single comment's thread. Return to all comments →
List comprehensions are so great!
Explanation
list a is simply a list of all the input data. It is sorted so that the lowest score is in the first index.
list b uses a list comprehension to remove all instances of the lowest score from list a. The lowest remaining score in the resulting list (found in the first index since it will be the first value taken from the already-sorted list a) is the second-lowest score overall.
list c then uses another list comprehension to trim down list b to only those scores that are equal to that second-lowest overall score.
list c is now a nested list containing the scores and names of the students that will make up the output. It is sorted alphabetically according to student name by using a lambda expression which indicates to use the contents of the second index of each object to be sorted ('name' in [score, name]) as the key for sorting.
Finally the names from list c are printed to satisfy the problem.