[Solved] TypeError: ‘float’ object is not subscriptable

‘float’ object is not subscriptable is a general python error message which can occur when you try to perform any kind of indexing operation on float type object.

In this article we will study different aspects of this error message as such what is meant by ‘float’ object is not subscriptable? Why you are getting this error message and how to resolve it. Go through this article thoroughly to get rid of this error message.

What is TypeError: ‘float’ object is not subscriptable?

'float' object is not subscriptable

Let’s try to decode the error message. We have divided the error message into three parts as below for better understanding:

TypeError: It means you are trying to perform an invalid operation on the variable of a particular type. If the operation is not supported by the type of variable then it will give TypeError.

float: float is a type of object. It tells us that an illegal operation is performed on the float type of object.

object is not subscriptable: It tells us that float type of objects are not subscriptable, which means you cannot perform indexing operation on float type of object.

What are subscriptable and non- subscriptable objects?

Subscriptable Objects:

If objects store multiple values and these values can be accessed using indexing then these are called subscriptable objects.

Subscriptable objects internally implements __getitem__() method. We can access elements of subscriptable objects using the index as shown below:

# Sample Python program to access first element from the list
quad =['United States','Japan','India','Australia']
print(quad[0])

When executed above code will give the following result.

United States

Here quad is a list type of object. We accessed the first element of a quad list using quad[0]

Strings, Lists, Tuples, and Dictionaries are the subscriptable objects in Python on which you can perform the indexing operation.

Non-Subscriptable Objects:

If object stores single or whole value then these are called non-subscriptable objects.

We cannot access elements of non-subscriptable objects using the index position. It will result in an error message saying the object is not subscriptable.

int and float are the Non-Subscriptables objects on which we cannot perform indexing operations.

Why you are getting TypeError: ‘float’ object is not subscriptable?

You are getting ‘float’ object is not subscriptable means in your code you are trying to get the value of a float type of object using indexing which is an invalid operation.

A float is a non-subscriptable object. Indexing operation is not supported on non-subscriptable objects.

Either you have mistakenly tried to access the value of a float variable using an index or if you want to access particular positions element of the value and directly used an index to get the element of value. That results in the TypeError: ‘float’ object is not subscriptable error message.

How to solve TypeError: ‘float’ object is not subscriptable?

Here we will see two scenarios where this error can occur and the way to resolve it. So don’t waste your time and start evaluating.

1. Access the first digit of a float value

Below sample code calculates the area of a rectangle by taking length and width as an input from the user. Once the area is calculated code tries to get the first digit of a calculated area using the index position.

# File: Sample_Program_1.py
# Sample Python program to calculate area of rectangle

# get input arguments from user for length and width 
length = float(input("Enter the length of a rectangle: "))
width = float(input("Enter the width of a rectangle: "))

# Area of rectangle is muliplication of width and length
area = length*width

#print first element of areaString values
print("Area of rectangle: " + str(area))
print("The first digit of an area: " + str(area[0]))

When the above code is executed it gives the following error message.

C:\Sample_Program>python Sample_Program_1.py
Enter the length of a rectangle: 24
Enter the width of a rectangle: 17
Area of rectangle: 408.0
Traceback (most recent call last):
File “C:\Sample_Program\Sample_Program_1.py”, line 13, in
print(“The first digit of an area: ” + str(area[0]))
TypeError: ‘float’ object is not subscriptable

We are getting this error message when the code tries to get the first digit of a calculated area using index i.e. area[0]. An area is a float type of object and it’s non-subscriptable, hence indexing operation cannot be performed on a float object.

Solution:

If you want to get the first digit of a calculated area then convert that float object into a string object first and then get the first character of string using index position as shown below.

# File: Sample_Program_1.py
# Sample Python program to perform append operation on the list

# get input arguments from user for length and width 
length = float(input("Enter length of a rectangle: "))
width = float(input("Enter width of a rectangle: "))

# Area of rectangle is muliplication of width and length
area = length*width

# Convert area into string
areaString = str(area)

#print first element of areaString values
print("Area of rectangle: " + areaString)
print("The first digit of an area: " + areaString[0])

When the above code is executed it will give the following result.

C:\Sample_Program>python Sample_Program_1.py
Enter the length of a rectangle: 24
Enter the width of a rectangle: 17
Area of rectangle: 408.0
The first digit of an area: 4

Note: string is subscriptable object hence it supports index operation

2. Accessing 2D values from a 1D list

In the following example, we are trying to access 2D values from a 1D list.

# File: Sample_Program_2.py
# Sample Python program to access values from the list object

# List object with some default values
valueList = [35.5, 41.956, 43.215, 54.884, 57.149]

#For loop to access all values of list object
for i in range(1, len(valueList)):
  iCnt = valueList[0][i]
  print(iCnt)

When the above code is executed it will give the following error message.

C:\Sample_Program>python Sample_Program_2.py
Traceback (most recent call last):
File “C:\Sample_Program\Sample_Program_2.py”, line 9, in
iCnt = valueList[0][i]
TypeError: ‘float’ object is not subscriptable

Solution:

Access values from a 1D list using the 1D index position as shown below:

# File: Sample_Program_2.py
# Sample Python program to access values from the list object

# List object with some default values
valueList = [35.5, 41.956, 43.215, 54.884, 57.149]

#For loop to access all the values of list object
for i in range(1, len(valueList)):
  iCnt = valueList[0],valueList[i]
  print(iCnt)

When the above code is executed it will give the following result.

C:\Sample_Program>python Sample_Program_2.py
(35.5, 41.956)
(35.5, 43.215)
(35.5, 54.884)
(35.5, 57.149)

Conclusion:

We hope this article was informative and you got a clear idea about what are subscriptable objects and non-subscriptable objects. Always be careful while accessing the index value of variables.  If you again face the same issue then make sure to validate all the code lines where you are trying to access the index value of an object.

For now, we are sure that you are able to get rid of an error message using the above methods. If you are still facing the issue then please do mention it in the comment section or you can reach out to us using the contact form. You can also contact us using our official mail id i.e. hello.technolads@gmail.com

Further Read:

TypeError: ‘builtin_function_or_method’ object is not subscriptable

Leave a Comment