Python sort list of dict by calculated value

By: Varghese Chacko 1 year, 1 month ago

We often needto sort a list of dictionaries by calculated value. Python's sorted is very powerful so that it can do literally anything with custom key function Assume that we have a list of 20 students with their name and marks. Wedont have the total of their marksin the list. We want to sort them by rank, ie descending order of total marks.

>>>students = [
    {'maths': 12, 'science': 10, 'language': 23, 'full_name': 'Anni Diloway'}, 
    {'maths': 11, 'science': 18, 'language': 19, 'full_name': 'Hastings Civitillo'}, 
    {'maths': 19, 'science': 13, 'language': 18, 'full_name': 'Beret Dulen'}, 
    {'maths': 18, 'science': 12, 'language': 10, 'full_name': 'Anton Hawkyens'}, 
    {'maths': 19, 'science': 22, 'language': 13, 'full_name': 'Sam Leehane'}, 
    {'maths': 12, 'science': 20, 'language': 24, 'full_name': 'Far Kendal'}, 
    {'maths': 11, 'science': 19, 'language': 10, 'full_name': 'Cullin Bulstrode'}, 
    {'maths': 16, 'science': 18, 'language': 13, 'full_name': 'Barron MacCague'}, 
    {'maths': 19, 'science': 9, 'language': 19, 'full_name': 'Patty Kubas'}, 
    {'maths': 14, 'science': 12, 'language': 9, 'full_name': 'Bale Stokey'}, 
    {'maths': 22, 'science': 13, 'language': 15, 'full_name': 'Chalmers Ege'}, 
    {'maths': 15, 'science': 11, 'language': 21, 'full_name': 'Valentine Breagan'}, 
    {'maths': 10, 'science': 10, 'language': 19, 'full_name': 'Nancy Loughhead'}
    {'maths': 24, 'science': 11, 'language': 14, 'full_name': 'Rosemaria Bilbee'}
    {'maths': 21, 'science': 24, 'language': 21, 'full_name': 'Carey Karpushkin'}
    {'maths': 25, 'science': 13, 'language': 23, 'full_name': 'Judye Adnam'}
    {'maths': 16, 'science': 23, 'language': 21, 'full_name': 'Eba Renn'}
    {'maths': 13, 'science': 15, 'language': 17, 'full_name': 'Eulalie Oxteby'}
    {'maths': 12, 'science': 21, 'language': 14, 'full_name': 'Laurena Licciardiello'}
    {'maths': 23, 'science': 15, 'language': 10, 'full_name': 'Armstrong Whatsize'}
    {'maths': 22, 'science': 16, 'language': 24, 'full_name': 'Gerry Burchfield'}
    {'maths': 13, 'science': 22, 'language': 20, 'full_name': 'Moselle Cummins'}
    {'maths': 11, 'science': 22, 'language': 18, 'full_name': 'Jacky Poxon'}
    {'maths': 23, 'science': 18, 'language': 10, 'full_name': 'Darryl Parkman'}
    {'maths': 22, 'science': 13, 'language': 9, 'full_name': 'Maribel Pattingson'}
]

Now let us define a method that calculate total of all all threemarks- Maths, Science and Language - andreturns it to be used as total.

>>> def sort_by_rank(d):
...     return d.get("science", 0) + d.get("maths", 0)+ d.get("language", 0)

Now just sort using sorted.

>>> sorted(students, key=sort_by_rank, reverse=True)
[
    {'maths': 21, 'science': 24, 'language': 21, 'full_name': 'Carey Karpushkin'}
    {'maths': 22, 'science': 16, 'language': 24, 'full_name': 'Gerry Burchfield'}
    {'maths': 25, 'science': 13, 'language': 23, 'full_name': 'Judye Adnam'}
    {'maths': 16, 'science': 23, 'language': 21, 'full_name': 'Eba Renn'}
    {'maths': 12, 'science': 20, 'language': 24, 'full_name': 'Far Kendal'}
    {'maths': 13, 'science': 22, 'language': 20, 'full_name': 'Moselle Cummins'}
    {'maths': 19, 'science': 22, 'language': 13, 'full_name': 'Sam Leehane'}
    {'maths': 11, 'science': 22, 'language': 18, 'full_name': 'Jacky Poxon'}
    {'maths': 23, 'science': 18, 'language': 10, 'full_name': 'Darryl Parkman'}
    {'maths': 19, 'science': 13, 'language': 18, 'full_name': 'Beret Dulen'}
    {'maths': 22, 'science': 13, 'language': 15, 'full_name': 'Chalmers Ege'}
    {'maths': 24, 'science': 11, 'language': 14, 'full_name': 'Rosemaria Bilbee'}
    {'maths': 11, 'science': 18, 'language': 19, 'full_name': 'Hastings Civitillo'}
    {'maths': 23, 'science': 15, 'language': 10, 'full_name': 'Armstrong Whatsize'}
    {'maths': 16, 'science': 18, 'language': 13, 'full_name': 'Barron MacCague'}
    {'maths': 19, 'science': 9, 'language': 19, 'full_name': 'Patty Kubas'}
    {'maths': 15, 'science': 11, 'language': 21, 'full_name': 'Valentine Breagan'}
    {'maths': 12, 'science': 21, 'language': 14, 'full_name': 'Laurena Licciardiello'}
    {'maths': 12, 'science': 10, 'language': 23, 'full_name': 'Anni Diloway'}
    {'maths': 13, 'science': 15, 'language': 17, 'full_name': 'Eulalie Oxteby'}
    {'maths': 22, 'science': 13, 'language': 9, 'full_name': 'Maribel Pattingson'}
    {'maths': 18, 'science': 12, 'language': 10, 'full_name': 'Anton Hawkyens'}
    {'maths': 11, 'science': 19, 'language': 10, 'full_name': 'Cullin Bulstrode'}
    {'maths': 10, 'science': 10, 'language': 19, 'full_name': 'Nancy Loughhead'}
    {'maths': 14, 'science': 12, 'language': 9, 'full_name': 'Bale Stokey'}
]

 Now the  student with maximum mark is on top of the list. :)