Python program to find average score of each students from dictionary of scores

PythonServer Side ProgrammingProgramming

Suppose we have a dictionary of students marks. The keys are names and the marks are list of numbers. We have to find the average of each students.

So, if the input is like scores = {'Amal' : [25,36,47,45],'Bimal' : [85,74,69,47],'Tarun' : [65,35,87,14],'Akash' : [74,12,36,75]}, then the output will be [38.25, 68.75, 50.25, 49.25] so 38.25 is average score for Amal, 68.75 is average score for Bimal and so on.

To solve this, we will follow these steps −

  • avg_scores := a new map
  • for each name in scores dictionary, do
    • avg_scores[name] := average of scores present in the list scores[name]
  • return list of all values of avg_scores


Let us see the following implementation to get better understanding

def solve(scores):
   avg_scores = dict()
   for name in scores:
      avg_scores[name] = sum(scores[name])/len(scores[name])

return list(avg_scores.values())

scores = {'Amal' : [25,36,47,45],'Bimal' : [85,74,69,47],'Tarun' : [65,35,87,14],'Akash' : [74,12,36,75]}




[38, 68, 50, 49]
Updated on 11-Oct-2021 10:20:43