If you are working with Python, you may encounter the error message “TypeError: ‘float’ object is not subscriptable” when trying to access a value at an index or slicing a float variable. This error occurs because float objects are not subscriptable, meaning you cannot access individual elements of a float object like you can with a list or a string.

In this tutorial, we will explore the causes of this error and provide several solutions to fix it. We will also discuss some best practices to avoid this error in the future. So, let’s get started!
Understanding the TypeError: 'float' object is not subscriptable
error
The error message here is quite helpful. It tells us that we’re trying to use a float as a subscriptible object (for example, a list, string, tuple, etc.) using the square brackets notation [] to retrieve a specific element or a slice of elements. A float object represents a floating point value and not a sequence and thus, we cannot really use them as subscriptible objects. If you try to do so, you’ll get this error.
Let’s look at an example.
# float variable temperature = 98.7 # using float as a subscriptable object print(temperature[0])
Output:
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[3], line 4 2 temperature = 98.7 3 # using float as a subscriptable object ----> 4 print(temperature[0]) TypeError: 'float' object is not subscriptable
In the above example, we created a float variable temperature
which stores the float value 98.7
. We then tried to access the value at index 0 in the variable temperature
and we get the error TypeError: 'float' object is not subscriptable
.
You’ll get the same error if you try to perform a slice operation on an integer value.
# float variable temperature = 98.7 # using float as a subscriptable object print(temperature[0:3])
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[4], line 4 2 temperature = 98.7 3 # using float as a subscriptable object ----> 4 print(temperature[0:3]) TypeError: 'float' object is not subscriptable
In the above scenarios, we are using the float variable as a subscriptable object which is not allowed (and doesn’t really make sense) since a float value represents a single value and not a sequence of values and thus a TypeError
is raised.
Fixing the error
To fix this error, you can either not use float objects as subscriptable or use a subscriptable object like a list instead.
Let’s revisit the examples from above and fix those errors.
# float variable temperature = 98.7 # using float as a subscriptable object print(temperature)
Output:
98.7
Here, we are not using the float value as subscriptable object and instead directly printing its value. You can see that we don’t get an error.
# list of temperatures temperatures = [91.1, 77.3, 82.0, 102.3, 89.6] # using list as a subscriptable object print(temperatures[0:3])
Output:
[91.1, 77.3, 82.0]
Here, instead of using a float value, we are using a which that is subscriptable, and thus performing operations like slicing or accessing a value at an index won’t result in this TypeError
.
Note that the solution to correct this error will depend on your use case and what you’re trying to achieve.
Conclusion
The “TypeError: ‘int’ object is not subscriptable” error occurs when we try to access an index of a float object or slice a float object, which is not possible as floats are not subscriptable. To fix this error, you can choose to not perform such operations on floats or use a subscriptable type like a list or a string instead.
You might also be interested in –
- How to Fix – TypeError ‘set’ object is not subscriptable
- How to Fix – TypeError ‘int’ object is not subscriptable