from statistics import mean
from operator import itemgetter
l = [['Hussain', 7, 8, 0], ['Max', 3, 4, 3], ['Alexandra', 10, 9, 8]]
print(sorted(([ele[0],mean(ele[1:])] for ele in l),key=itemgetter(1),reverse=True))
[['Alexandra', 9.0], ['Hussain', 5.0], ['Max', 3.3333333333333335]]
If you are not using python >= 3.4 you can just do the average calculation manually.
print(sorted(([ele[0],sum((ele[1:])) / len(l) - 1] for ele in l),key=itemgetter(1),reverse=True))
ele[0],mean(ele[1:])
takes the name and the remaining elements which are the scores, we then sort using itemgetter(1)
which is the score/second element as the key and setting reverse=True
to go from high to low.
I would also use itertools.islice
to get the slice avoiding building new lists with normal slicing:
from itertools import islice
print(sorted(([ele[0],mean(islice(ele,1,None))] for ele in l),key=itemgetter(1),reverse=True))
Or without using mean:
from itertools import islice
print(sorted(([ele[0],sum(islice(ele,1,None)) / len(l) - 1] for ele in l),key=itemgetter(1),reverse=True))