Open In App

Fix ‘Int’ Object is Not Subscriptable in Python

Last Updated : 18 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will study how to fix ‘int’ object that is not subscriptable in Python. But before that let us understand why it occurs and what it means.

What is ‘Int’ Object Is Not Subscriptable Error?

The error ‘int’ object is not subscriptable occurs when you attempt to use indexing or slicing on an integer, a data type that doesn’t support these operations.

As we know integer in Python is a data type that represents a whole number. Unlike lists or dictionaries, integers do not hold a sequence of elements and therefore do not support indexing or slicing.
For example, if x = 42 (an integer), and we try to do something like x[0], it’s an attempt to access the first element of x as if x were a list or a tuple. Since integers don’t contain a collection of items, this operation isn’t valid and you get a TypeError: ‘int’ object is not subscriptable.

Example

Python3




# Example causing 'int' object is not subscriptable error
x = 42
# Attempting to use subscript notation on an integer
print(x[0])


Output:

Hangup (SIGHUP)
Traceback (most recent call last):
File "Solution.py", line 4, in <module>
print(number[0])
TypeError: 'int' object is not subscriptable

Why Does ‘Int’ Object Is Not Subscriptable Error Occur?

The ‘Int’ object is not subscriptable error in Python arises due to specific characteristics of integer (int) objects. Here are reasons why this error occurs:

  • Immutability of Integers
  • Function Return Type Mismatch
  • No Iterable Structure

Immutability of Integers

As we know that Integers in Python are immutable, meaning their values cannot be changed after creation and subscripting or indexing operations are applicable to mutable sequences (e.g., lists, strings), where elements can be accessed or modified using indices.

Since integers are not mutable sequences, attempting to use square brackets for subscripting results in the ‘Int’ object is not subscriptable error.

Python3




# Example triggering 'Int' object is not subscriptable error
num = 42
value = num[0# Error: 'Int' object is not subscriptable


Output:

Hangup (SIGHUP)
Traceback (most recent call last):
File "Solution.py", line 3, in <module>
value = num[0] # Error: 'Int' object is not subscriptable
TypeError: 'int' object is not subscriptable

Function Return Type Mismatch

In this function is expected to return a list or tuple when condition is False, as indicated by the else branch.

However, in the else branch, the function returns an integer instead of a list or tuple which results in ‘Int’ Object Is Not Subscriptable error

Python3




def get_data(condition):
    """
    This function is expected to return a list or tuple,
    but under certain conditions, it returns an integer.
    """
    if condition:
        return [1, 2, 3# Returns a list
    else:
        return 42  # Returns an integer
  
# Function call with a condition that leads to an integer being returned
result = get_data(False)
  
# Attempting to index the result, which is an integer in this case
first_element = result[0]


Output:

Hangup (SIGHUP)
Traceback (most recent call last):
File "Solution.py", line 15, in <module>
first_element = result[0]
TypeError: 'int' object is not subscriptable

No Iterable Structure

As we know integers lack the iterable structure required for subscripting. Iterable objects, such as lists or strings, have a well-defined sequence of elements that can be accessed using indices.

Attempting to use square brackets on an integer implies treating it as if it has iterable properties, resulting in the ‘Int’ object is not subscriptable error.

Python3




# Example demonstrating misinterpretation of syntax
integer_value = 123
value = integer_value[0


Output:

Hangup (SIGHUP)
Traceback (most recent call last):
File "Solution.py", line 3, in <module>
value = integer_value[0] # Error: 'Int' object is not subscriptable
TypeError: 'int' object is not subscriptable

Solve ‘Int’ Object Is Not Subscriptable In Python

  • Use Strings or Lists Instead of Integers
  • Check Variable Types
  • Review Code Logic:

Let us study them in detail

Use Strings or Lists Instead of Integers

In Python, subscript notation is applicable to strings and lists. So we can convert integer to a string or list before using subscript notation.

Python3




# Converting integer to string and using subscript notation
number = 42
number_str = str(number)
print(number_str[0])


Output

4


Check Variable Types

We need to make sure that the variable we are using is of the expected type we want it to be. If it’s supposed to be a sequence (string or list), make sure it is not mistakenly assigned an integer value.

Python3




# Checking variable type before using subscript notation
number = 42
if isinstance(number, (str, list)):
    print(number[0])
else:
    print(
        f"Error: Variable type '{type(number).__name__}' is not subscriptable.")


Output

Error: Variable type 'int' is not subscriptable.


Review Code Logic

Examine your code logic to determine if subscript notation is genuinely necessary. If not, revise the code to avoid subscripting integers.

Python3




# Reviewing code logic to avoid subscripting integers
number = 42
number_str = str(number)
print(number_str[0])


Output

4


Conclusion

From the above information we can say TypeError: ‘int’ object is not subscriptable error in Python typically happens due to a type mismatch where an integer is mistakenly treated as a subscriptable object like a list or tuple. So in order to prevent this, it’s crucial to consistently check data types, especially when dealing with dynamic or complex data structures.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads