[SOLVED] Matching 2 different lists with highest values in Python

Issue

This Content is from Stack Overflow. Question asked by Osama Ajmal

Array_student = [Joe, Peter, Andy, Tom, Pat]

Array_marks = [14, 9, 6, 8, 12]

Find the highest marks in the array and print the name of the student with the highest marks.

Without using any function such as max().



Solution

You should use {} dict where the student name will be the key and the marks will be the value and then use sorting and take the first item of the new list

data = {'joe':14, 'Peter': 9, 'Andy': 6, 'Tom' : 8, 'Pat' : 12}
sort_data = sorted(data.items(), key=lambda x: x[1], reverse=True)
next(iter(sort_data)) # outputs 'Peter'


This Question was asked in StackOverflow by Osama Ajmal and Answered by Mr_yassh It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?