Lists and For Loops Exercise - Don't Understand why index is -1 rather than 4

Screen Link:
https://app.dataquest.io/c/77/m/422/lists-and-for-loops/4/retrieving-multiple-list-elements

My Code:

fb_rating_data = [row_1[0], row_1[3], row_1[4]]
insta_rating_data = [row_2[0], row_2[3], row_2[4]]
pandora_rating_data = [row_3[0], row_3[3], row_3[4]]
avg_rating = (fb_rating_data[2] + insta_rating_data[2] + pandora_rating_data[2]) / 3

What I expected to happen:
I expected this code to be the correct answer

What actually happened:

avg_rating is greater than what we expected.

I understand that my answer differs from the correct answer in that the first list contained in the variable fb_rating_data contains a list as below:

fb_rating_data = [row_1[0], row_1[3], row_1[-1]]

However, as the other variables contain the same number of indexed lists as fb_rating_data, and point to the same index within each respective list, I don’t understand why the index for user ratings in the list contained within fb_rating_data in the correct answer is -1 rather than 4 as in my answer. How do you determine that the user rating in fb_rating_data is -1 rather than 4?

Thanks!

1 Like

Hi @ratnesh.bh! The index -1 indicates the last element of a list. In your case, the indices 4 and -1 are equivalent because the lists are 5 elements long.

For example, if you write print(row_1[-1] == row_1[4]) the output will be True. Have a look at what row_1[-1] and row_1[4] are by printing out their values.

Next, the error tells you that the average rating is greater than expected, so you should search for an error in your calculations of the average rating. If you don’t succeed, please post your full answer here so that I can have a look :smile:

1 Like

Hi Artur, thanks! Your explanation made sense and I changed my code however I see that it matches the correct answer but I am getting the same error.

Here’s my code:

row_1 = ['Facebook', 0.0, 'USD', 2974676, 3.5]
row_2 = ['Instagram', 0.0, 'USD', 2161558, 4.5]
row_3 = ['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
row_4 = ['Temple Run', 0.0, 'USD', 1724546, 4.5]
row_5 = ['Pandora - Music & Radio', 0.0, 'USD', 1126879, 4.0]

fb_rating_data = [row_1[0], row_1[3], row_1[-1]]
insta_rating_data = [row_2[0], row_2[3], row_2[4]]
pandora_rating_data = [row_3[0], row_3[3], row_3[4]]


avg_rating = (fb_rating_data[2] + insta_rating_data[2] + pandora_rating_data[2]) / 3

And the error is still

avg_rating is greater than what we expected.

Any thoughts?

Look closely at the the pandora_rating_data variable. You have an error there :slight_smile:

1 Like