Python TypeError: ‘float’ object is not subscriptable Solution

Posted in /  

Python TypeError: ‘float’ object is not subscriptable Solution
vinaykhatri

Vinay Khatri
Last updated on April 14, 2024

    Python has three subscriptable objects list , string , and tuples . They are subscriptable because all these objects support indexing to access their elements or items. But Python objects like float does not support indexing and are non-subscriptable objects.

    If we perform indexing to access any float value, we will receive TypeError: 'float' object is not subscriptable error in Python.

    In this Python guide, we will walk through the above error and discuss how to solve it. We will also discuss a common example where many new Python learners encounter this error.

    So now, let's get started.

    Python Error - TypeError: 'float' object is not subscriptable

    When we use square brackets to access a float-type object using index numbers, we encounter the error TypeError: 'float' object is not subscriptable .

    It is divided into two parts Error Type and Error Message.

    1. Error Type ( TypeError ): TypeError occurs in Python when we perform an invalid operation on a Python data type object.
    2. Error message ( 'float' object is not subscriptable ): This error message tells us we are accessing a floating-point value or variable as a subscriptable object. And it generally occurs when we use indexing on a floating-point number.

    Example

    >>> #floating point number
    
    >>> float_num = 34.8
    
    >>> #using indexing on the float number
    
    >>> float_num[0]
    
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: 'float' object is not subscriptable

    Common Error Scenarios

    Let us discuss some common scenarios where you can encounter this error.

    Scenario 1: Accessing an Item from a Float

    Many new Python learners mistake the indexing operation of the string, list, and tuple with float numbers when they need to solve problems like extracting the first or last digit from a floating-point number.

    Example

    #floating point number
    float_num = 3453.97
    
    #access first digit of the floating number using indexing
    first_digit = float_num[0]
    
    print(first_digit)

    Output

    Traceback (most recent call last):
    File "main.py", line 5, in <module>
    first_digit = float_num[0]
    TypeError: 'float' object is not subscriptable

    Break the code

    In the above example, we get this error because we tried to access the floating-type variable's float_num first digit using indexing, which is invalid in Python. We can not perform indexing on a floating-point number. That's why Python threw the error 'float' object is not subscriptable .

    Solution

    To solve the above problem, we first need to change the floating-point number to a string to get the first digit using indexing. Then we will convert that first digit number back to an integer number using the Python int() function.

    #floating point number
    float_num = 3453.97
    
    #convert the float to string
    str_num = str(float_num)
    
    # access the first digit using indexing
    first_digit = str_num[0]
    
    # convert the firt digit string value back to integer
    first_digit = int(first_digit)
    
    print(f"The first digit of {float_num} is: {first_digit}")

    Output

    The first digit of 3453.97 is: 3

    Scenario 2: Replacing Multiple Items

    Python list supports slicing, which returns a new list of a few items from the original list. When we want to replace multiple items in a list, slicing comes in handy.

    Example

    price = input("Enter the price of the pen: ")
    pen = [2.50, 2.75, 1.80, 1.75, 2.60]
    pen[0][1][2][3][4] = [float(price)] * 5
    print(pen)

    Output

    Enter the price of the pen: 6
    Traceback (most recent call last):
      File "/home/main.py", line 3, in <module>
        pen[0][1][2][3][4] = [float(price)] * 5
    TypeError: 'float' object is not subscriptable

    Break the Code

    In the above example, we tried to update the items in the list pen at once using the value entered by the user. However, in line 3, we used the incorrect syntax. The indices should be separate operations rather than a single chained operation. When the code tries to change the values in the pen list, we receive the error TypeError: 'float' object is not subscriptable .

    The above code tries to retrieve the values at the index positions [0], [1], [2], [3], and [4]. But these values exist in the float type in our list, which is the cause of the error.

    To resolve this, one of the solutions is to use slicing.

    Let us write the correct code to get the desired output.

    Solution

    price = input("Enter the price of the pen: ")
    pen = [2.50, 2.75, 1.80, 1.75, 2.60]
    pen[0:5] = [float(price)] * 5
    print(pen)

    Output

    Enter the price of the pen: 4
    [4.0, 4.0, 4.0, 4.0, 4.0]

    Wrapping Up!

    The Python ‘float’ object is not subscriptable Error is a TypeError that occurs when we try to access a floating-point number using indexing. Only Python lists, tuples, and string support indexing, and primitive values like int and float throw an error when we perform indexing on them.

    We hope the above article has helped you understand the error in detail. Analyze the above example scenarios to get a clear idea of when the error occurs and how to resolve it. If you encounter the same error and are unable to resolve it, comment down your code, and we will help you with debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments