Error: List assignment index out of range _ Data Science Fundamental Course

Screen Link:

My Code:

reviews_max = {}

for app in apps_data_google[1:]:
    name = app[0]
    del apps_data_google[10473]
    
    n_reviews = float(app[3])

    if name in reviews_max and reviews_max[name] < n_reviews:
        reviews_max[name] = n_reviews

    elif name not in reviews_max:
        reviews_max[name] = n_reviews
print('Number in reviews_max:', len(reviews_max))
print('\n')
print(len(apps_data_google))


What I expected to happen:
it should successfully removed that 3.0M ( the wrong data) and succesfully converted to the float data.

What actually happened:

IndexError                                Traceback (most recent call last)
<ipython-input-10-16f2fb9051ec> in <module>
      3 for app in apps_data_google[1:]:
      4     name = app[0]
----> 5     del apps_data_google[10473]
      6 
      7     n_reviews = float(app[3])

IndexError: list assignment index out of range

I am also wondering how do we locate, how do we exactly know that the wrong data is on row 10473?? and I dont understand why my list assignment index is out of range as I already checked the length of apps_data_goole , it is 10841 , thank you guys in advance!

Please make sure that you are also providing the link to the Mission in your post.

1 Like

Sorry, the link is here:

https://app.dataquest.io/m/350/guided-project%3A-profitable-app-profiles-for-the-app-store-and-google-play-markets/5/removing-duplicate-entries-part-two

Hope that is correct link, thank you again

The issue is that you have del apps_data_google[10473] inside of your for loop.

So, at each iteration of your loop, you are deleting the row at the index 10473. Once that row is deleted, row 10474 moves to row 10473 and then that row will get deleted at the next iteration.

That cycle continues till you have less than 10473 rows left, and you get that error.

You only need to delete that row once and not as part of a loop. The instructions point this out as well -

Make sure you don’t run the del statement more than once, otherwise you’ll delete more than one row.

The instructions link the discussion which points this out. There are similar discussions that you can find that explain this as well.

2 Likes

thank you so much!!! I will try to fix it and let you know