Looping through list of list

Screen Link: https://app.dataquest.io/m/422/lists-and-for-loops/6/list-of-lists

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]

for list in app_data_set:
    for e in list:
        if list.index([e])==-1:

What I expected to happen:
I want to get the rating or last element of the list of list. For that I am using For loop. While looping I am not able to get the index of the element. I want to compare the index of the element I am looping to -1 which is the location of the element in every list. This is not part of the actual question but I am trying to apply the learning from the chapter I have learned later in the course.

What actually happened:

ValueErrorTraceback (most recent call last)
<ipython-input-1-d1e2a08f81af> in <module>()
     11     for e in list:
     12         print(e)
---> 13         if list.index([e])==-1:
     14             print(e)
     15 print(list1)

ValueError: ['Facebook'] is not in list
Basically I want to loop through list of list and get the desired element. Once I get the element at the correct location I want to put it into another list.

hi @bhatiabhavya05

Welcome to the DataQuest Community!

We are glad to know you are trying to implement your learnings to find solutions in a different manner than suggested. :ok_hand:

Regarding the code if list.index([e]) == -1: the index() function takes the element so no need for square brackets.

so the error can be avoided if we replace with list.index(e) itself.

However this code won’t be work because you have already split the list into separate elements using the code for e in list: so if you print this your result would be like:


What you are attempting to do can be done by using list[-1] and appending this to your new list variable, in your first for loop itself.

1 Like