Find Average Of a key with multiple values
Question: I have a dictionary e.g:

    
    
    Jhon: 3, 4, 5
    Mark: 5, 5, 5
    Matthew: 7, 8 , 9
    

This is my code:

    
    
    from statistics import mean
    class1 = {}
    for line in f:
                columns = line.split(":")
                if len(columns) == 2:
                    names = columns[0]
                    scores = columns[1].strip()
                else:
                    pass
                if class1.get(names):
                    class1[names].append(scores)
                else:
                    class1[names] = list(scores)
    

I have already tried:

    
    
    for k, v in class1.items()
       print("{} : {scores}".format(names , mean(scores)
    

I get the error:

    
    
    TypeError: unsupported operand type(s) for +: 'int' and 'str'
    

And I have also tried:

    
    
    for k, v in class1.items():
       average = float(sum(v))/len(v)
    for k, v in class1.items():
       print("{} : {}".format(names=k, average=v)
    

The error is:

    
    
    Traceback (most recent call last):
      File "C:/Users/Onyeka/PycharmProjects/untitled/task3.py", line 59, in <module>
        print("{} : {scores}".format(names , mean(scores)))
      File "C:\Python34\lib\statistics.py", line 331, in mean
        T, total, count = _sum(data)
      File "C:\Python34\lib\statistics.py", line 161, in _sum
        for n,d in map(_exact_ratio, values):
      File "C:\Python34\lib\statistics.py", line 247, in _exact_ratio
        raise TypeError(msg.format(type(x).__name__))
    TypeError: can't convert type 'str' to numerator/denominator
    

I want the output to be like this

    
    
       Jhon: 4
       Mark: 3
       Matthew: 8
    

Would it also be possible to print these averages in order from highest to
lowest?


Answer: I'm a little confused about some of what you've posted, including strange
formatting for your dictionary, but I think I understand basically what you
want. This is how I solved it:

    
    
    from statistics import mean
    
    f = {'Jhon': [3, 4, 5], 'Mark': [5, 5, 5], 'Matthew': [7, 8 , 9]}
    averages = {}
    for key in f:
        averages[key] = int(mean(f[key]))
    
    print(averages)
    

Output is:

    
    
    {'Matthew': 8, 'Mark': 5, 'Jhon': 4}
    

