bassyproductionz
bassyproductionz

Reputation: 63

How do I perform calculations with sublists in python

I'm trying to get 4 average numbers from the 4 sublists from my main list. I'm getting the error: TypeError: list indices must be integers or slices, not list. Can someone explain my what that means in this context?

studentgrades = [ [95, 92, 86],[66, 75, 54],[89, 72, 100],[34, 0, 0] ]

antw = []
for student in studentgrades:
     average = sum(studentgrades[student]) / len(studentgrades[student])
     antw.append(average)


print(antw)

Upvotes: 0

Views: 217

Answers (4)

vash_the_stampede
vash_the_stampede

Reputation: 4606

Where you went wrong was going a little to far with this, your original function will work fine if you take the extra elements you added off. In your loop students is representing already each individual element, so there is no need to call it again as studentgrades[student]

antw = []
for student in studentgrades:
     average = sum(student) / len(student)
     antw.append(average)

This can be done using list comprehension or a single loop appending to a list

studentgrades = [ [95, 92, 86],[66, 75, 54],[89, 72, 100],[34, 0, 0] ]
l = [round(sum(i)/len(i), 2) for i in studentgrades]
# [91.0, 65.0, 87.0, 11.33]

Expanded

l = []
for i in studentgrades:
    total = sum(i)
    grades = len(i)
    average = total/grades
    l.append(round(average, 2))

Another option would be map that allows us to apply the function to all the list elements

l = list(map(lambda x: round(sum(x)/len(x), 2), studentgrades))

Upvotes: 1

user9190538
user9190538

Reputation: 11

Because student is a list, not the numerical index, you should use

for i in range(len(studentGrid)):
   antw.append(avg(studentGrid[i]))

Upvotes: 1

jedwards
jedwards

Reputation: 30210

Your loop, for student in studentgrades: will set, on each iteration, student to be one of the sublists.

So for the first iteration, student will be the sublist [95, 92, 86].

So the next line,

average = sum(studentgrades[student]) / len(studentgrades[student])

Will raise that error, as you're attempting to index (access) a list (studentgrades) by using another list (student).

One way to fix this would be to iterate over the studentgrades list (as you're doing) then use that list in your average calculation:

for student in studentgrades:
    average = sum(student) / len(student)
    antw.append(average)

To put that visually, that code would result in a first iteration that'd look like:

for student in studentgrades:
    average = sum([95, 92, 86]) / len([95, 92, 86])
    antw.append(average)

If you really wanted to index studentgrades every time, you might change your code to something like:

for index in range(len(studentgrades)):
    average = sum(studentgrades[index]) / len(studentgrades[index])
    antw.append(average)

But the first option of iterating over the sublists (instead of the output of a range generator) seems much better :)

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249223

studentgrades[student] does not make sense, because student is a list of scores. You can just use student (better name it student_scores or something).

Upvotes: 0

Related Questions