Python For Loops- Screen 3/11

Why does the for loop function extract the individual element from the data set, in this case, ratings? Though if we see logically when we enter row[3] in the loop body, it should extract all the elements of row_4 and not the 4th element of all rows??

Hello @mursuljamil

The app_data_set is a list of lists. If it was just a single list, your logic which you have mentioned above would be right. Since, it’s is a list of lists, when the for loop runs for the first time, it goes through, the first list:

['Facebook', 0.0, 'USD', 2974676, 3.5]

And it extracts the 4th element of this, and when the loop runs for the second time, it goes through the second list, and does the same. I hope, I have answered your question.

1 Like

Can you please exemplify when you say, “The app_data_set is a list of lists. If it was just a single list, your logic which you have mentioned above would be right.”

If you inspect the app_data_set variable, you will see that it is a list of lists like:

[['Facebook', 0.0, 'USD', 2974676, 3.5], ['Instagram', 0.0, 'USD', 2161558, 4.5], ['Clash of Clans', 0.0, 'USD', 2130805, 4.5], ['Fruit Ninja Classic', 1.99, 'USD', 698516, 4.5], ['Minecraft: Pocket Edition', 6.99, 'USD', 522012, 4.5]]

So when the loop runs for the first time, it goes through the first element of the list, which is:

['Facebook', 0.0, 'USD', 2974676, 3.5]

Depending on the index which you are extracting, the loop will extract from this list. When it runs for the second time, it goes through the second list and does the same till the end of the list of lists. So, that’s why it extracts all the 4th element of the lists.

Here’s a tool which I used when I got confused with iterations. It visualizes your Python code.

Thanks a lot! This tool for visualizing seems quite useful : )

2 Likes

Your tool looks really cool, @vishallbabu5! :grinning: Can you please share it in our Resources subcategory so that other learners could also easily find it? Thank you! :pray:

1 Like

Shared it in the resource category, Elena. :smiley:

1 Like