‘int’ object is not subscriptable – Python Error


‘int’ object is not subscriptable is a Python type error which occurs when you try to access “index” on int variables.

a = 1

This code will throw, 'int' object is not subscriptable error because here we have assigned a = 1 which means a is an integer. In the next line, we are printing the 0th index of a, print(str(a[0])), which is incorrect.

The error screen will look like this –

python error int object is not subscriptable

In type unsafe languages like javascript, php, python etc., we can assign int to float or int to string or string to boolean but it doesn’t work for arrays.

Arrays needs to be defined explicitly because, internally, they are not a value else the location of values. It means an array variable holds the address in memory location from where your first element starts.

So, the right way of implementing the above code is –

a = ['Ironman', 'Captain America', 'Thor', 'Hulk']
// Output: Ironman

Python will throw the same error if you try to access a multidimensional index from single dimensional array. Or more precisely, large dimensional index than the maximum array dimension.

a = ['Ironman', 'Captain America', 'Thor', 'Hulk']
// Error: 'int' object is not subscriptable

    Tweet this to help others

Live Demo

The live demo might not work in private window.

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like

Convert list to string in Python

Table of Contents Hide Joining with delimiterJoining integer itemsUsing map() functionUsing lambda functionUsing LoopsLive DemoRelated Posts: Python provides join() function which could be used to convert list to string. If…
View Post