DEV Community

Coder
Coder

Posted on

TypeError: 'int' object is not subscriptable in Python

If you are a beginner in Python programming, then you may have come across the TypeError: 'int' object is not subscriptable message at some point in time. This error message can appear when trying to access a variable or method within a list, tuple, or dictionary. It can be frustrating, especially when you do not know what it means.

However, you do not have to worry because this blog post will help you understand the TypeError: 'int' object is not subscriptable error message and how to fix it.

Understanding the Error Message

When you encounter the TypeError: 'int' object is not subscriptable in Python, it means that you are attempting to use the subscripting operator ([]) to access an element within an object that is not subscriptable. An object is considered subscriptable if it is a list, tuple or dictionary.

To better understand this, let us look at some examples:

# Example 1 - accessing a variable within a list
my_list = [1, 2, 3, 4]
print(my_list[0]) # Result: 1

# Example 2 - accessing a variable within a tuple
my_tuple = (1, 2, 3, 4)
print(my_tuple[0]) # Result: 1

# Example 3 - accessing a variable within a dictionary
my_dict = {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
print(my_dict[1]) # Result: 'one'
Enter fullscreen mode Exit fullscreen mode

In the examples above, we are accessing elements within a list, tuple and dictionary respectively. The subscripting operator ([]) is used to access these elements, and it works fine because the objects are subscriptable.

However, if we try to access an element within an object that is not subscriptable, we get the TypeError: 'int' object is not subscriptable error message.

# Example 4 - attempting to access a variable within an integer
my_int = 5
print(my_int[0]) # TypeError: 'int' object is not subscriptable
Enter fullscreen mode Exit fullscreen mode

In the example above, we are attempting to access an element within an integer, which is not subscriptable. Hence, we get the TypeError: 'int' object is not subscriptable error message.

Common Causes of TypeError: 'int' object is not subscriptable Error

The TypeError: 'int' object is not subscriptable message can be caused by various things, including:

Using a Variable Instead of a List, Tuple or Dictionary

As we have seen in the previous section, the subscripting operator ([]) can only be used to access elements within a list, tuple or dictionary. If you try to use it on an object that is not subscriptable, such as an integer, you will get the TypeError: 'int' object is not subscriptable message.

# Example 5 - using an integer instead of a list
my_int = 5
print(my_int[0]) # TypeError: 'int' object is not subscriptable

# Example 6 - using a string instead of a list
my_string = 'hello'
print(my_string[0]) # Result: 'h'
Enter fullscreen mode Exit fullscreen mode

In the examples above, we are trying to access the first element of an integer and a string using the subscripting operator ([]). It works fine with the string but results in an error with the integer.

Using an Invalid Index or Key

If you try to access an index or key that is not present in a list, tuple or dictionary, you will get the TypeError: 'int' object is not subscriptable message.

# Example 7 - using an invalid index on a list
my_list = [1, 2, 3, 4]
print(my_list[5]) # IndexError: list index out of range

# Example 8 - using a non-existent key on a dictionary
my_dict = {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
print(my_dict[5]) # KeyError: 5
Enter fullscreen mode Exit fullscreen mode

In the examples above, we are trying to access an index that is out of range in a list and a key that does not exist in a dictionary. These actions cause errors.

Using the Wrong Data Type

Another common cause of the TypeError: 'int' object is not subscriptable message is using the wrong data type.

# Example 9 - using a string instead of an integer
my_int = '5'
print(my_int[0]) # Result: '5'

# Example 10 - using a float instead of an integer
my_int = 5.0
print(my_int[0]) # TypeError: 'float' object is not subscriptable
Enter fullscreen mode Exit fullscreen mode

In the examples above, we are trying to access the first element of a string and a float using the subscripting operator ([]). It works fine with the string but results in an error with the float.

How to Fix the TypeError: 'int' object is not subscriptable Error

Now that you understand what the TypeError: 'int' object is not subscriptable message means and the common causes, let us look at how you can fix it.

Check That You Are Using a List, Tuple or Dictionary

The first thing you need to do when you encounter the TypeError: 'int' object is not subscriptable message is to check that you are using a list, tuple or dictionary. If you are using an integer or another non-subscriptable object, you need to fix the code to use one of the subscriptable objects.

# Example 11 - using a list instead of an integer
my_list = [5]
print(my_list[0]) # Result: 5
Enter fullscreen mode Exit fullscreen mode

In the example above, we fixed the code by using a list instead of an integer. Now, we can access the element using the subscripting operator ([]).

Check That You Are Using a Valid Index or Key

If you are using a list, tuple or dictionary, but you still get the TypeError: 'int' object is not subscriptable message, you need to check that you are using a valid index or key.

# Example 12 - using a valid index on a list
my_list = [1, 2, 3, 4]
print(my_list[3]) # Result: 4

# Example 13 - using a valid key on a dictionary
my_dict = {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
print(my_dict[3]) # Result: 'three'
Enter fullscreen mode Exit fullscreen mode

In the examples above, we are using valid indexes and keys to access elements within a list and a dictionary, respectively. Therefore, we do not get the TypeError: 'int' object is not subscriptable message.

Check That You Are Using the Correct Data Type

If you are using a subscriptable object, and you are using a valid index or key, but you still get the TypeError: 'int' object is not subscriptable message, you need to check that you are using the correct data type.

# Example 14 - using an integer instead of a float
my_float = 5.0
print(int(my_float)[0]) # Result: 5
Enter fullscreen mode Exit fullscreen mode

In the example above, we fixed the code by converting the float to an integer using the int() function. Now, we can access the element using the subscripting operator ([]).

Conclusion

The TypeError: 'int' object is not subscriptable message can be frustrating, especially for beginners in Python programming. However, you do not have to worry because this blog post has explained what the message means and how to fix it.

When you encounter the TypeError: 'int' object is not subscriptable message, the first thing you need to do is check that you are using a subscriptable object. If you are using a non-subscriptable object, you need to fix the code to use one of the subscriptable objects.

You also need to check that you are using a valid index or key and the correct data type. If you follow these steps, you will be able to fix the TypeError: 'int' object is not subscriptable error message and write better Python code.

Top comments (0)