'Compute the average app rating for the apps stored in the app_data_set variable

I want to calculate the sum of all the rating from the app_data_set list of lists and store it in rating_sum.

My code is adding up only the "row_1" rating 5 times and storing it in rating_sum instead of adding rating from each row.

row_1 = ['Facebook', 0.0, '$', 2974676, 3.5]
row_2 = ['Instagram', 0.0, '$', 2161558, 4.5]
row_3 = ['Clash of Clans', 0.0, '$', 2130805, 4.5]
row_4 = ['Temple Run', 0.0, '$', 1724546, 4.5]
row_5 = ['Pandora - Music & Radio', 0.0, '$', 1126879, 4.0]
app_data_set = [row_1, row_2, row_3, row_4, row_5 ]
rating_sum = 0
for rating in app_data_set:
    rating = app_data_set[0][4]
    rating_sum = rating_sum + app_data_set[0][4]
    print(rating_sum)

I expect the output to be:

3.5 
8.0
12.5
16.5
20.5

Actual output is:

3.5
7.0
10.5
14.0
17.5


Solution 1:[1]

Try using this loop instead:

for rating in app_data_set:
    rating_sum += rating[-1]
    print(rating_sum)

Which gives:

3.5
8.0
12.5
17.0
21.0

(P.S. your output is wrong, mine should be correct, do the math)

Solution 2:[2]

rating_sum=0
for rating in app_data_set:
    rating_sum += rating[-2]
    print(rating_sum)

its sum of (total no of reviews ) is a different column in the table.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 U12-Forward
Solution 2 Blastfurnace