Dictionary | How Can I Find Who Has The Highest Average Grade Using Dictionary?

In this post, we will learn How can I find who has the highest average grade using a dictionary where we have direct built in functions like sum and diving by the total number of elements we can get the average so here we have taken an example and a pseudo code to explain the same.



As we know in the dictionary we save the data in the form of key value pairs as each value is mapped with a particular key, here we have taken the value of a dictionary and added them to get the average of the values stored in the dictionary.

students = {
    'Alice': [90, 85, 92],
    'Bob': [80, 85, 90],
    'Charlie': [95, 90, 98],
    'Dave': [75, 80, 85]

highest_avg = -1
highest_  student = ''

for student, grades in students.items():
    avg = sum( grades) / len( grades)
    if avg > highest_ avg:
        highest_ avg = avg
        highest_ student = student

print( f'{ highest_ student} has the highest average grade of { highest_ avg: .2f}')

Here first we have to take a dictionary which stores let marks for some students and the number of students is four. To get the average first we added all the marks of each student which are stored in sum and then divided by the number of subjects for each student.

we calculated the average so that we do not need to check for the max with all the elements as the highest will be always greater than average so now we only need to look at the marks which are greater than average.

And each time when we find marks greater than the average the variable which started the average will be replaced with the new highest it will continue till we get to reach the last element of the dictionary.

Finally, the above code will print a message indicating which student has the highest average grade and what that grade is, using string formatting to display the values with two decimal places.

Provided here we have assumed as every student has nonzero marks and that every column has some marks otherwise it (the code) might give an error as ‘zero division error’.

To learn more about python problems and solutions along with concepts tutorials visit: How To Pass-Variables From A Java & Python Client To A Linux/Ubuntu Server Which Is Running C?

To learn more about different other programming languages like Java, MongoDB, MySQL, and python as the solutions to the problems faced during solving it and to get clear on the concepts related to them go ahead with: beta python programming languages Solutions

Leave a Comment

%d bloggers like this: