Stuck on TypeError: " 'int' object is not subscriptable " in Guided Project: Prison Break

Screen Link:

My Code:

for row in data:
    data[index] = row[:-1]
    index += 1

print(data[:3])

What I expected to happen:

What actually happened:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-65-65f34b9f7576> in <module>
      1 index = 0
      2 for row in data:
----> 3     data[index] = row[:-1]
      4     index += 1

TypeError: 'int' object is not subscriptable

Hello everyone! I just got started with my first guided project: Prison Break, and I’m already having some trouble. I’m trying to remove the last element of each row in the data (Details), and when I ran the code, I got TypeError: ‘int’ object is not subscriptable. I checked everything and I don’t see anything wrong in line 3 or anywhere else. Is there something I missed?

[My First Data Science Project](https://my_first_data_science_project.ipynb)

Hi @dregner02 , a google search will give you this answer

The TypeError: ‘int’ object is not subscriptable error occurs if we try to index or slice the integer as if it is a subscriptable object like list, dict, or string objects.
https://itsmycode.com/python-typeerror-int-object-is-not-subscriptable/

Does this make any sense to you? Can you think of any reason why this could be happening?

Have you printed out data and does it look fine?

Please use these suggestions as a hint to look into the problem. You will be able to solve it. If not please do share the previous lines of codes as well. The link you shared didn’t get uploaded it seems.

Hello, I was able to figure it out. I created a new notebook and retyped all of my code and it was all running normally. I noticed that the lines of code were ordered from top to bottom (In[1], In[2], In[3], etc.) and in my previous notebook, they weren’t ordered from top to bottom (In[21], In[13], In[10], etc.). That might not have been the solution, but it was the only thing different I noticed when running my code in a new notebook. Thanks for the help!

1 Like

Hi @dregner02. It sounds like that might have been your problem all along: code cells were run in a non-sequential order. This can cause variables to be overwritten and cause confusing outputs, including your error.

I think you may have been able to solve this problem by simply re-running all the cells in your original notebook. This is actually a good thing to do from time to time to make sure your code runs without error.

To re-run all the cells in your notebook, select Run All from the Cell menu in Jupyter Notebook. You may also want to do a Restart & Clear Output under the Kernel menu before running all the cells again.

1 Like