In Python, you cannot access values inside a float using indexing syntax. Floating-point numbers are single values. Indexing syntax is suitable for subscriptable objects such as strings or lists. If you attempt to retrieve an individual number from a float, you will raise the “TypeError: ‘float’ object is not subscriptable”.

This tutorial will go through what the error means and the possible causes. We will explore examples and provide solutions for each of them.


TypeError: ‘float’ object is not subscriptable

Let’s break up the error message to understand what the error means. TypeError occurs whenever you attempt to use an illegal operation for a specific data type. The part “float object” tells us the error concerns an illegal operation for floating-point numbers.

The part “is not subscriptable” tells us we cannot access an element of the float object.

Subscriptable objects contain a collection of objects, and we access these objects by indexing.

You cannot retrieve a specific value from a float. Floating-point numbers are not subscriptable objects.

Integers are also not subscriptable objects, and if you try to index an integer, you will through TypeError: ‘int’ object is not subscriptable.

Possible causes of “TypeError: ‘float’ object is not subscriptable” are:

  • Accessing an item from a float
  • Replacing multiple items in a list

Example #1: Accessing an Item from a Float

You may encounter this error when using the indexing operation on float numbers, such as extracting the first or last digit from a floating-point number. Let’s look at an example of defining a floating-point number a try to access the first digit:

# floating point number

float_number = 456.0

# Access first digit of the floating number using indexing

first_digit = float_number[0]

# Print output

print(first_digit)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
first_digit = float_number[0]

TypeError: 'float' object is not subscriptable

The Python interpreter raises the TypeError because we are trying to access an element in a floating-point number.

Solution

We need to change the floating-point number to a subscriptable type to solve the problem. We will typecast the floating-point number to a string using the str() function. Once we have a string, we can access the first digit using indexing. Then we can convert the first digit string to an integer using the int() function.

# floating point number

float_number = 456.0

# Convert float to string

string_number = str(float_number)

# Access the first digit using indexing

first_digit = string_number[0]

# Convert first digit string value to integer

first_digit = int(first_digit)

# Print the first digit to console
print(f'The first digit of {float_number} is: {first_digit}')

Let’s run the code to get the result:

The first digit of  456.0 is: 4

Example #2: Replacing Multiple Items in a List

Slicing involves specifying a list of items in an object that you want to access or change. We can use list slicing to replace multiple items in a list. Let’s look at an example of a program that changes the weights of apples in grams to a particular weight. The first part of the program takes an input from the user asking for the default weight of an apple:

weight = input("Enter the default weight for an apple:  ")

We can then define a list of apple weights we need to change

apples = [96.3, 103.5, 74.5, 84.0, 90.2]

We can try to change the values in the list using indexing:

apples[0][1][2][3][4] = [float(weight)]*5
print(apples)

The above code resets the weights of the apples in the list at all index positions to the value of weight, a floating-point number. The weight is enclosed in square brackets and multiplied by five. This operation assigns five values to the five index positions in the apples list.

Let’s see what happens when we run the program:

Enter the default weight for an apple:  80
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
apples[0][1][2][3][4] = [float(weight)]*5

TypeError: 'float' object is not subscriptable

The Python interpreter raises the TypeError because the list only contains floats. Therefore, when we access the first element of the list with apples[0] we get a float. It follows that apples[0][1] is equivalent to accessing the element at index position 1 in a float.

Solution

We can use the slicing syntax to update the list to solve this error.

weight = input("Enter the default weight for an apple:  ")

apples = [96.3, 103.5, 74.5, 84.0, 90.2]

apples[:4] = [float(weight)]*5

print(apples)
Enter the default weight for an apple:  80

[80.0, 80.0, 80.0, 80.0, 80.0]

The code retrieves all the items in the list from the index position 0 to 4 and assigns each value with the input value of weight. Go to the post titled “How to Get a Substring From a String in Python” to learn more about slicing.

Summary

Congratulations on reading to the end of this tutorial. The error “TypeError: ‘float’ object is not subscriptable” occurs when you try to access a floating-point number like a list. To solve this error, ensure you only use indexing or slicing syntax on a subscriptable object, like a list or a string. If you need to change multiple values in a list, ensure you use slicing instead of specifying a list of index numbers.

For further reading on TypeErrors, go to the articles:

To learn more about Python for data science and machine learning, you can go to the online courses page on Python for the most comprehensive courses.

Have fun and happy researching!