How to get the average of a row in a 2D list in python?

Viewed 11901

I'm new to Python and I'm working with a 2D list and not exactly sure how to get the average of the rows.

For example I have this list:

myList = [[70, 80, 90], [30, 40, 50]]

and I would like to get the average of the first and second row.

Something like this:

(70 + 80 + 90)/3 = 80

(30 + 40 + 50)/3 = 40

I'm implementing my print_student_average function, but I'm a little lost. Someone tell me what I'm doing wrong please.

Here's my code:

def main():
    myList = [[70, 80, 90], [30, 40, 50]]

    print(print_student_average(myList))
    print_exam_average(myList)

def print_student_average(myList):

    total_sum = [sum(i) for i in range(len(myList))]
    average = total_sum/3

    return average

def print_exam_average(myList):

    col_totals = [ sum(x)/2 for x in zip(*myList) ]

    for col in col_totals:
        print("the average of the exam is: ", col)


main()
4 Answers
Related