I of course don't know your use case, but it seems like it would be better to store the information in a new "inverted dict" instead of creating a list of separate lists of students without any score information attached to them:
marks_inverted = {v: [] for v in marks.values()}
for k,v in marks.items():
marks_inverted[v].append(k)
print(marks_inverted)
#OUTPUT: {6: ['john'], 5: ['suzan', 'ann'], 4: ['bob', 'joe', 'peter'], 3: ['fred']}
#Note that as a dict, marks_inverted doesn't have any order
Now you can look up the list of students who got each score:
print(marks_inverted[6])
#OUTPUT: ['john']
Or the maximum score:
print(marks_inverted[max(marks_inverted)]
#OUTPUT: ['john']
If you use a collections.defaultdict
instead of a regular dict
, you won't have to worry about getting an error if you ask for a score nobody got, and you don't have to initialize the empty lists first (idea for this came from here):
from collections import defaultdict as dd
marks_inverted = dd(list)
for k,v in marks.items():
marks_inverted[v].append(k)
#if marks_inverted[v] doesn't exist it is initialized as [] before it is appended
Now if you look up 2, you won't get an error even though nobody got a 2:
print(marks_inverted[2])
#OUTPUT: []
If you're using a collections.defaultdict
and you want to get the students who performed over a range of scores, you can do it like this:
[marks_inverted[k] for k in range(2,5) if marks_inverted[k]]
#OUTPUT: [['fred'], ['bob', 'joe', 'peter']]
...or if you want the list flattened:
[name for index in [marks_inverted[k] for k in range(2,5) if marks_inverted[k]] for name in index]
#OUTPUT: ['fred', 'bob', 'joe', 'peter']
(The if marks_inverted[k]
part of the statement makes it so no empty lists are included as members of the list output.)
If at any time you really do need to get rid of the score information and just have a list of students in order of highest to lowest marks (like in the original question), you can just do:
[marks_inverted[k] for k in reversed(sorted(marks_inverted)) if marks_inverted[k]]
#OUTPUT: [['john'], ['ann', 'suzan'], ['bob', 'joe', 'peter'], ['fred']]