{ }codelper

Error Messages › Python

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])
When you run it
Traceback (most recent call last):
  File "main.py", line 2, in <module>
TypeError: 'int' object is not subscriptable

Edit and run it →

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.

n = 1234s = str(n)print(s[0])print(int(s[0]) + int(s[-1]))
Output
1
5

Edit and run 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])
When you run it
Traceback (most recent call last):
  File "main.py", line 3, in <module>
TypeError: 'NoneType' object is not subscriptable

Edit and run it →

Fixed code

Write a.sort() on a line by itself. If you want a new sorted list back, use sorted(a).

a = [3, 1, 2]a.sort()print(a[0])b = sorted([9, 7, 8])print(b[0])
Output
1
7

Edit and run it →

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])
When you run it
Traceback (most recent call last):
  File "main.py", line 2, in <module>
TypeError: 'builtin_function_or_method' object is not subscriptable

Edit and run it →

Fixed code

Parentheses go after a function name, square brackets after a list name.

a = [10, 20, 30]print(len(a))print(a[len(a) - 1])
Output
3
30

Edit and run it →

What to check when you see this error

  1. Look at the word in quotes in the error message. 'int' is an integer, 'float' is a decimal number, 'NoneType' is None, and 'function' is a function.
  2. On the line where the error happened (the number after line), find the name right before the [. Add print(type(name)) above that line to see what it holds.
  3. Check that you did not put a different value into a variable that used to be a list. For example, after a = len(a) or a = a.sort(), a is no longer a list.
  4. Inside a loop, check whether the value you took out is already a number. In for x in a:, x is not the list but one value from it.

Practice problems

See also