-3
def get_grades(grades: List[list]) -> int:
    """Return the name of the student that has the highest grade.
    If there is a tie for the highest grade, it should return the name that appears
    first in grades."""

My data set is a list of lists e.g.

GRADES = [['Todd', 'Biology', 67, 5], ['Ben', 'Chemistry', '88', 7]]

Here, the grade lies at index 2 of each sublist.

How do I do this?

6
  • Is there any way you can get the data in a different form? Commented Oct 27, 2017 at 17:30
  • 1
    is it 67.004 or 67,004 ? Commented Oct 27, 2017 at 17:30
  • 4
    Welcome to SO. Unfortunately this isn't a discussion forum, tutorial, or code writing service. Please take the time to read How to Ask and the links it contains. You should spend some time working your way through the Tutorial, practicing the examples. It will give you an introduction to the tools Python has to offer and you may even start to get ideas for solving your problem. Commented Oct 27, 2017 at 17:30
  • 1
    possible dup of: stackoverflow.com/questions/39748916/… and stackoverflow.com/questions/6193498/… Commented Oct 27, 2017 at 17:35
  • Create a placeholder for the person with the highest grade and fill it with a fake person with a grade of zero; iterate over your list; for each person in the list check to see if the grade is greater than the placeholder's grade; if it is, assign the person to the placeholder. Commented Oct 27, 2017 at 17:35

2 Answers 2

3

You can try this

lst=[['susan', 'adams' , 67, 004], ['garret', 'jimmy', 88, 9]]
max(lst, key=lambda i: i[2])[0]

output

>>> lst=[['susan', 'adams' , 67, 004], ['garret', 'jimmy', 50, 9]]
>>> max(lst, key=lambda i: i[2])[0]
'susan'

The built-in function max can take a key-function argument to help it make its decision. It works the same as a sorting key-function as described in the Sorting How To.

Sign up to request clarification or add additional context in comments.

3 Comments

I think the grade is i[2].
Yes , changed now
@DanielTrugman , i have indexed [0] and it returns only name
0

This will return a list of all the names that got the highest grade:

info = [['susan', 'adams' , 67, 4], ['garret', 'jimmy', 88, 9], ['stack', 'overflow', 88, 11]]
max_grade = max(person[2] for person in info)
max_persons = [person[0] for person in info if person[2] == max_grade]

If info is not an empty list, max_persons[0] will always contains a name of one of those who got the highest grade

1 Comment

Maybe unwind the generator expression and the list comprehension to make it easier for the OP .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.