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:
--------------------------------------------------------------------------- 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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
--------------------------------------------------------------------------- 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 –