Multiple list elements

Screen Link: https://app.dataquest.io/c/114/m/608/python-lists/7/retrieving-multiple-list-elements-

My Code:

total_rating = fb_rating_data[-1] + insta_rating_data[-1] + minecraft_rating_data[-1]

What I expected to happen:

What actually happened:

Replace this line with the output/error

<in the answer it is given as,
total_rating = fb_rating_data[2] + insta_rating_data[2] + minecraft_rating_data[2]>
while indexing last element, it can be either -1 or 4. (a row containing five elements)
why it is indexed as 2 in the “answer”?

You will have to include the rest of your code as well because it’s unclear how you have defined those individual lists - fb_rating_data, insta_rating_data, minecraft_rating_data

sorry. I thought the link will be sufficient enough to explain my doubts.

<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 = [‘Fruit Ninja Classic’, 1.99, ‘USD’, 698516, 4.5]
row_5 = [‘Minecraft: Pocket Edition’, 6.99, ‘USD’, 522012, 4.5]

fb_rating_data = [row_1[0], row_1[3], row_1[-1]]
insta_rating_data = [row_2[0], row_2[3], row_2[-1]]
minecraft_rating_data = [row_5[0], row_5[3], row_5[-1]]

total_rating = fb_rating_data[-1] + insta_rating_data[-1] + minecraft_rating_data[-1]

average_rating = total_rating / 3>

In answer,
<fb_rating_data = [row_1[0], row_1[3], row_1[-1]]
insta_rating_data = [row_2[0], row_2[3], row_2[-1]]
minecraft_rating_data = [row_5[0], row_5[3], row_5[4]] # row_5[4] is the same as row_5[-1]

total_rating = fb_rating_data[2] + insta_rating_data[2] + minecraft_rating_data[2]
average_rating = total_rating / 3>

I would be grateful for any assistance.

fb_rating_data is a list with 3 items. So, the indices to access the items would be 0, 1, 2.

From the previous Screen in the lesson, you learned:

Negative indexing: the last element has the index number -1, the second to last element has the index number -2, and so on.

So, the last element can be accessed using the index 2 or -1. The same applies to the other two lists as well.

The index 4 would only apply to lists that have 5 elements.

Let me know if it’s clear or not.

Also, for future reference, use the Reply button in the bottom-right of the comment you are replying to and not the blue Reply button close to the bottom of the page. The former results in a notification, but the latter might not.

2 Likes

got it. thank you for clearing my doubts

I don’t still understand his explanation. Please kindly break down. Thank you

Here, we are extracting the data( rating of Fb, insta, Minecraft ) from the list created for three apps separately.

< fb_rating_data = [row_1[0], row_1[3], row_1[-1]]
insta_rating_data = [row_2[0], row_2[3], row_2[-1]]
minecraft_rating_data = [row_5[0], row_5[3], row_5[-1]] >

these lists have the indices 0, 1, 2. To calculate total_rating, we need the last element. The last element has an index of 2 or -1.
This explanation may clear your doubt. Pls let me know.

It’s clearer now. Thank you