0

I have a list in python 3.6.5 which goes as follows

a_list = [['name1', 5, 6, 3, 7], ['name3', 2, 6, 3, 6], ['name3', 2, 10, 8, 5]]

So it's basically name, score1, score2 etc.

I want to add up the scores and rank they names highest to lowest

So far, I have

a_list.sort(a_list, key=lambda x: x[not sure what to put here], reverse=True)

I need a way of adding up the numbers in the list without affecting the str field.

How can I achieve this?

2
  • 1
    Are you meant to have duplicate names? Commented May 23, 2018 at 11:17
  • how does the desired output look like for the example you posted? Commented May 23, 2018 at 11:21

1 Answer 1

2

This should work:

a_list = [['name1', 5, 6, 3, 7], ['name3', 2, 6, 3, 6], ['name3', 2, 10, 8, 5]]
a_list.sort(key=lambda x: sum(x[1:]), reverse=True)
print(a_list)

Output:

[['name3', 2, 10, 8, 5], ['name1', 5, 6, 3, 7], ['name3', 2, 6, 3, 6]]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.