All Python errors
TypeError in Python: 'NoneType' is not subscriptable
This message means square brackets were applied to None, which has no items. To fix it, find where the variable got its value: methods such as sort() and append() change a list in place and return None.
Code with the error
times = [52, 47, 60]
ordered = times.sort()
print(ordered[0])
Error message (Python 3.12, last line)
Corrected code
times = [52, 47, 60]
ordered = sorted(times)
print(ordered[0])
Output
47
Common causes
- The result of list.sort(), list.reverse() or list.append() is saved to a variable.
- A function has no return, so it gives None.
- A function returns a value only in one branch of if.
- dict.get() did not find the key and returned None.
Practise in the course
Lesson: ListsFrequently asked questions
What does subscriptable mean?
A value you can use with square brackets, such as a list, a string or a dictionary.
What is the difference between sort() and sorted()?
sort() reorders the list itself and returns None; sorted() returns a new sorted list.