Sum values in nested lists python

Viewed 3247

I have a list:

l = [['a', 10, 30], ['b', 34, 89], ['c', 40, 60],['d',30,20]]

where the first item inside each sublist is the name and other two number are marks (sub1 and sub2)

The nested lists can be dynamic, i.e the number of nested lists can change according to function.

What I am looking for is to find

average of subj 1 i.e (10+34+40+30)/4 and 
similarly sub2 (30+89+60+20)/4

also average marks of a: (10+30)/2
average marks of b: (34+89)/2  and so on.

I tried:

c = 0
for i in range(0,len(list_marks1)):
    c += list_marks1[i][1]
sub_1avg = float(c)/len(list_marks1)
d = 0
for i in range(0,len(list_marks1)):
    d += list_marks1[i][2]
sub_2avg = float(d)/len(list_marks1)

but this is not correct.

Is there any optimal way to do this? Since the number of subjects in my nested lists can also change.

3 Answers
Related