What This Error Means and Why It Happens

The "TypeError: 'int' object is not subscriptable" error occurs when your Python code tries to use square brackets to access part of an integer, the same way you would access an item in a list or a character in a string. Integers are single values — they don't have parts you can pull out with brackets, so Python stops and throws this error.

The most common cause is treating an integer as if it were a list, string, or dictionary. For example, if you write my_number[0] where my_number is an integer like 5, Python will reject it because you cannot subscript (index into) a number.

A second common cause is accidentally assigning an integer to a variable that you later try to use as a list or string. This often happens when a function returns a single number instead of the collection you expected, or when a loop variable gets reassigned to a number partway through your code.

Key Takeaways

  • This error means you are using square brackets on an integer value, which Python does not allow because integers have no parts to access.
  • Check the line number in the error message first — that is where the brackets are being applied to an integer.
  • Use type() to print what kind of value a variable actually holds, so you can see whether it is an integer or a list.
  • Convert the integer to a string with str() if you need to access individual digits, or wrap it in a list with [my_number] if you need it to behave like a collection.

Reading the Error Message to Find the Problem

Python's error message tells you exactly which line caused the problem. The message will say something like TypeError: 'int' object is not subscriptable and then show a line number and the code that failed. Go to that line first — that is where you are using square brackets on an integer.

The traceback (the list of function calls leading up to the error) shows you the path through your code. If the error happens inside a function you called, read upward through the traceback to see which line called that function. The integer value came from somewhere — usually from a return statement, a loop variable, or an assignment — and the traceback helps you trace it back.

Using type() to Check What You Actually Have

Before you try to subscript a variable, print its type to confirm it is what you think it is. Add a line like print(type(my_variable)) right before the line that fails. This will tell you whether the variable is an integer, a list, a string, or something else.

If the output says <class 'int'>, you have an integer and cannot use brackets on it. If it says <class 'list'> or <class 'str'>, then something else is wrong — perhaps the index you are using is out of range, or the variable got reassigned somewhere you did not notice.

Converting an Integer to a String to Access Digits

If you need to work with individual digits of a number — for example, to get the first digit of 12345 — convert the integer to a string first using str(). Then you can use brackets: str(12345)[0] returns '1' (as a string, not a number).

Remember that the result is a string character, not an integer. If you need it as a number, convert it back with int(): int(str(12345)[0]) gives you 1 as an integer. This approach works for any integer, including negative numbers — just be aware that str(-123)[0] returns '-', the minus sign.

Wrapping an Integer in a List When You Need a Collection

If your code expects a list but you have a single integer, wrap it in square brackets to create a one-item list: [my_number]. Now you can use subscript notation: [my_number][0] returns the integer itself.

This is useful when a function or loop expects a collection as input. For example, if a function is written to loop through items in a list, but you only have one number to pass, wrapping it in a list lets the function work without rewriting it. Just remember that [5][0] returns 5, but [5][1] will fail because there is no second item.

Common Scenarios Where This Error Appears

One frequent case is when a function returns a single integer instead of a list, but your code assumes it returns a list. For example, len(my_list) returns an integer (the count of items), not a list. If you then try to do len(my_list)[0], you get this error because you are subscripting the count, not the list itself.

Another scenario is a loop variable that gets reassigned. You might loop through a list with for item in my_list:, but then later in the loop, item gets set to an integer (perhaps from a calculation or a function call). If you later try to subscript item, it fails because it is no longer the list item you started with.

A third case is dictionary or API responses where you expect a nested structure but get a flat value. For example, if you fetch data from an API and expect data['results'][0] to be a list, but data['results'] is actually just an integer, subscripting it will fail.

Debugging Steps to Solve It

Start by adding print() statements around the line that fails. Print the variable name, its type, and its value: print(f"my_var = {my_var}, type = {type(my_var)}"). This shows you exactly what you are working with.

Next, trace backward to find where the integer came from. Did a function return it? Did a loop assign it? Did you reassign the variable by accident? Once you know the source, you can decide whether to convert it (using str() or wrapping it in a list), change how you subscript it, or rewrite the logic to handle both integers and collections.

If the variable should be a list but is an integer, check the function or line that created it. Does it need to return a list instead? Should you wrap the integer in a list before using it? Should you check the type before subscripting? The fix depends on what your code is supposed to do.

Frequently Asked Questions

Can I subscript a float the same way I subscript an integer?

No. Floats (decimal numbers like 3.14) are also not subscriptable. The error message would say 'float' object is not subscriptable, but the fix is the same: convert to a string if you need individual digits, or wrap in a list if you need a collection.

What if the error says the object is not subscriptable but does not mention 'int'?

The same principle applies to any type that is not subscriptable — floats, booleans, None, and custom objects without a __getitem__ method. Check what type the variable actually is using type(), then decide whether to convert it or change how you access it.

Does wrapping an integer in a list change its value?

No. [5] is a list containing the integer 5. The integer itself is unchanged. When you subscript it with [5][0], you get back the original integer 5. The list is just a container.

Why does my function return an integer when I expected a list?

Check the function's documentation or code. Some functions are designed to return a single value (like len() or sum()), while others return collections. If a function should return a list but returns an integer, either the function needs to be fixed, or your code needs to handle the integer differently than you planned.