Python 'int' object is not subscriptable
TypeError: 'int' object is not subscriptable
Getting an item with square brackets, as in value[index], only works on values like lists, strings and dictionaries. If you use square brackets on a value with nothing inside it, such as a number or None, you get this error. The word in quotes in the error message ('int', 'NoneType' and so on) is the type of the value that was in front of the brackets.
Cause 1. You tried to get a digit out of a number
An integer is a single piece, so you cannot get its first digit with n[0]. This often happens when you want to look at each digit of a number you read with int(input()).
Wrong code
n = 1234print(n[0])Traceback (most recent call last): File "main.py", line 2, in <module> TypeError: 'int' object is not subscriptable
Fixed code
To look at each digit, turn the number into a string with str(). What you get out is a character, so turn it back with int() if you want to calculate with it.
Cause 2. You thought it was a list, but it is None
If the error message is 'NoneType' object is not subscriptable, the value in front of the brackets is None. sort(), append() and reverse() change the list in place and return nothing. If you store their result in a variable, it holds None.
Wrong code
a = [3, 1, 2]a = a.sort()print(a[0])Traceback (most recent call last): File "main.py", line 3, in <module> TypeError: 'NoneType' object is not subscriptable
Fixed code
Write a.sort() on a line by itself. If you want a new sorted list back, use sorted(a).
Cause 3. You used square brackets to call a function
Functions are called with parentheses. Writing len[a] with square brackets means "get something out of the function len". In that case the word in quotes is 'builtin_function_or_method' or 'function'.
Wrong code
a = [10, 20, 30]print(len[a])Traceback (most recent call last): File "main.py", line 2, in <module> TypeError: 'builtin_function_or_method' object is not subscriptable
Fixed code
Parentheses go after a function name, square brackets after a list name.
What to check when you see this error
- Look at the word in quotes in the error message.
'int'is an integer,'float'is a decimal number,'NoneType'isNone, and'function'is a function. - On the line where the error happened (the number after
line), find the name right before the[. Addprint(type(name))above that line to see what it holds. - Check that you did not put a different value into a variable that used to be a list. For example, after
a = len(a)ora = a.sort(),ais no longer a list. - Inside a loop, check whether the value you took out is already a number. In
for x in a:,xis not the list but one value from it.
Practice problems
See also
- Python IndexError: list index out of range
- Python TypeError: 'int' object is not iterable
- Python TypeError: 'int' object is not callable
- Python 'NoneType' object has no attribute
- Syntax: Python Slicing: [start:stop:step] Explained
- Syntax: Python sort() vs sorted(): Sort a List
- Learn it step by step: Python Lists and arrays
- All Python error messages
- Online Python interpreter