This error means you tried to use square brackets on a number
The error "TypeError: 'int' object is not subscriptable" appears when your Python code tries to access part of an integer using square brackets, the way you would access part of a list or string. Integers don't work that way — they're single values, not collections you can index into.
When Python sees code like my_number[0] or my_number[1], it expects my_number to be something it can slice into: a list, string, dictionary, or tuple. If my_number is actually an integer, Python stops and throws this error.
This mistake usually happens in three situations: you're indexing the wrong variable, you're indexing a function result that returned a number instead of what you expected, or you're looping through data and accidentally treating a single number as if it were a collection.
Key Takeaways
- Square brackets [] only work on collections like lists, strings, and dictionaries — not on integers or floats.
- Check what type your variable actually is by adding print(type(variable_name)) before the line that fails.
- If a function returns a number when you expected a list, the function itself may be wrong, or you may be calling it on the wrong data.
- When looping through data, make sure you're indexing the collection itself, not the individual items you pull from it.
The three most common causes
The first cause is indexing a variable that turned out to be a number. You might have written code that works with a list in one case but receives an integer in another. For example, if a function sometimes returns a single number and sometimes returns a list of numbers, and you always try to access the first item with [0], the code will crash when it gets the single number.
The second cause is calling a function that returns a number, then trying to index that result. If you write len(my_list)[0], you're trying to index the result of len(), which is always an integer. The code should be my_list[0] instead — you want the first item of the list, not the length of the list.
The third cause is a loop where you're pulling individual items from a collection and then trying to index those items. If you write for item in my_list: print(item[0]) and each item is a number rather than a string or list, the error will fire on the first loop.
How to find which line is causing it
Python's error message tells you the exact line number where the crash happened. Look at that line and find every place square brackets appear. Then check what's inside the brackets — what variable or expression are you trying to index?
Add a line right before the crash to print the type and value of that variable. For example, if the crash is on line 15 where you wrote result[0], add this on line 14:
print(type(result), result)
Run the code again. The output will show you whether result is actually an integer, and what its value is. Once you know the type, you can decide whether you should be indexing it at all, or whether you need to change how you got that variable in the first place.
Fixing it when you indexed the wrong variable
If you meant to index a list but accidentally indexed a number, change the variable name. For example, if you wrote count[0] but meant to get the first item from items, change it to items[0].
If the variable name is correct but it's the wrong type, trace back to where that variable was created or assigned. A function may have returned a number when you expected a list, or an earlier line may have overwritten a list with a single value. Fix the source, not the symptom.
Fixing it when a function returned a number
If you're indexing the result of a function call, check what that function actually returns. Functions like len(), sum(), count(), and index() all return integers, not collections. You cannot index them.
If you need the first item of a list, index the list itself: my_list[0]. If you need to know how many items are in a list, use len(my_list) but don't try to index the result. If you need to find where an item appears in a list, use my_list.index(item) to get the position, but again, don't index the result — the result is already the position you wanted.
Fixing it when looping through data
When you loop through a collection with for item in collection:, each item is a single element pulled from the collection. If the collection contains numbers, then item is a number, and you cannot index it.
If you need to index items in the loop, either loop through the positions instead of the items, or make sure each item is actually a collection. For example:
Wrong: for number in my_numbers: print(number[0]) — crashes if my_numbers contains integers.
Right: for number in my_numbers: print(number) — just print the number itself.
Also right: for i in range(len(my_numbers)): print(my_numbers[i][0]) — loop through positions and index both the outer list and the inner item, if each inner item is itself a collection.
Frequently Asked Questions
Can I convert an integer to a list so I can index it?
You can convert an integer to a string with str(my_number), and then index the string to get individual digits. For example, str(123)[0] gives you '1'. But this is rarely what you want. Usually the real fix is to stop trying to index the number and instead use it as a value.
What if the variable is sometimes a list and sometimes a number?
Check the type before you index it. Use if isinstance(my_variable, list): to run different code depending on whether it's a list. Or fix the function that's returning inconsistent types — it should always return the same type, or you should handle both cases intentionally.
Does this error mean my data is wrong?
Not necessarily. The error means your code is trying to do something that doesn't make sense with the data it received. The data might be correct and your code might be wrong, or the data might be wrong and your code might be right. Use the type-checking step to figure out which one.
Why does Python allow indexing on some types but not integers?
Indexing means accessing a specific position within an ordered collection. Integers are single values with no positions inside them, so indexing doesn't make sense. Lists, strings, and tuples are ordered collections, so indexing works — you can ask for the item at position 0, position 1, and so on.