-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?

Abby Liu
  • 13
  • 5
  • Is there any way you can get the data in a different form? – PeterH Oct 27 '17 at 17:30
  • 1
    is it 67.004 or 67,004 ? – Sandeep Lade Oct 27 '17 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 [ask] and the links it contains. You should spend some time working your way through [the Tutorial](https://docs.python.org/3/tutorial/index.html), 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. – wwii Oct 27 '17 at 17:30
  • 1
    possible dup of: https://stackoverflow.com/questions/39748916/find-maximum-value-and-index-in-a-python-list and https://stackoverflow.com/questions/6193498/pythonic-way-to-find-maximum-value-and-its-index-in-a-list – Nir Alfasi Oct 27 '17 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. – wwii Oct 27 '17 at 17:35
  • Please show us your attempts so far... – Lamar Latrell Oct 27 '17 at 18:09

2 Answers2

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.

wwii
  • 23,232
  • 7
  • 37
  • 77
Sandeep Lade
  • 1,865
  • 2
  • 13
  • 23
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

Daniel Trugman
  • 8,186
  • 20
  • 41
  • Maybe *unwind* the generator expression and the list comprehension to make it easier for the OP . – wwii Oct 27 '17 at 17:38