doer_123 - 3 months ago 7x

Python Question

I need help trying to find the student who has the highest total scores.

`s1= {'A': [100, 95, 100], 'B': [100, 100, 100], 'C': [95, 95, 80], 'D': [100, 100, 80]}`

def wrtd2():

for k, v in s1.iteritems():

total = 0

for i in v:

total = total + i

print total

wrtd2()

But it is printing the total for all students.

Answer

Try using `max`

with a custom key function:

```
def wrtd2(s):
return max(list(s.items()), key=lambda x: sum(x[1]))[0]
s1 = {'A': [100, 95, 100], 'B': [100, 100, 100], 'C': [95, 95, 80], 'D': [100, 100, 80]}
print(wrtd2(s1))
```

this will print

```
B
```

Source (Stackoverflow)

Comments