Python TypeError: list indices must be integers or slices, not float Solution

Posted in /  

Python TypeError: list indices must be integers or slices, not float Solution
vinaykhatri

Vinay Khatri
Last updated on May 30, 2024

    In Python, we use index numbers to access an individual element from a list . The index number of every list object starts from 0 to n-1, where n is the total number of elements present in the list.

    Index numbers are integer values. If we use a float number as an index value, we encounter an error TypeError: list indices must be integers or slices, not float .

    This guide will discuss the above error in detail, learn why it occurs, and how to solve it. We will also walk through a common example explaining this error and a solution.

    So let's get started!

    Python Problem: TypeError: list indices must be integers or slices, not float

    Python list is a data structure that stores elements of different data types sequentially. We can use the index numbers, which are integer values, to access an individual or a sequence of elements from the list. Whenever you insert an element in the list, it is assigned with the index value.

    Apart from integers, we also have floating-point values to represent numerical data. But if we pass the floating-point value as an index number, Python raises the error TypeError: list indices must be integers or slices, not float . This error statement has two parts.

    1. TypeError (Exception Type)
    2. list indices must be integers or slices, not float (Error Message)

    1. TypeError

    TypeError is one of Python's standard exceptions. It arises when we perform an unsupported operation or function on an inappropriate type. Also, this error occurs when we pass an invalid data type argument to a method or function.

    2. list indices must be integers or slices, not float

    The error message indicates that the index number needs to be an integer value or a slice syntax and not float. We encounter this error message when we pass a floating-point number inside the square bracket to access the list element.

    Here is an example where you encounter the error.

    Example

    # list
    my_list = [10, 20, 30, 40, 50]
    
    # float number
    index = 2.0
    
    print(my_list[index])

    Output

    Traceback (most recent call last):
      File "main.py", line 8, in 
        print(my_list[index])
    TypeError: list indices must be integers or slices, not float

    In the above example, we get this error because we passed the float value 2.0 as an index value to the list my_list . Python list does not accept floating-point numbers as index values.

    Common Example Scenario

    Let's say we have a list top3 that contains the information of the top 3 students from a class. We need to write a program that accepts a number between 0 to 2 and returns the particular student's information.

    Example

    top3= [
                ['1','Rahul', '990', '17'],
                ['2','Ravi', '987', '17'],
                ['3','Anil', '967', '17'],
          ]
    
    # convert the input number in float
    rank = float(input("Enter a Number between 0 to 2: "))
    
    print("Rank:", top3[rank][0])
    print("Name:", top3[rank][1])
    print("Marks", top3[rank][2])
    print("Age", top3[rank][3])

    Output

    Enter a Number between 0 to 2: 1
    Traceback (most recent call last):
      File "main.py", line 10, in <module>
       print("Rank:", top3[rank][0])
    TypeError: list indices must be integers or slices, not float

    Break the code

    In the above example, we get the error in line 10 with print("Rank:", top3[rank][0]) statement. This is because the value of rank in that line is 1.0 , which is a floating-point number. While accepting the input from the user, we convert it into a float with the float() function, and use it to access the top3 list items.

    Solution

    If we accept a number from the user side as an index value, we always convert that user input into an integer number using the int() function.

    top3= [
                ['1','Rahul', '990', '17'],
                ['2','Ravi', '987', '17'],
                ['3','Anil', '967', '17'],
          ]
    
    # convert the input number in integer
    rank = int(input("Enter a Number between 0 to 2: "))
    
    print("Rank:", top3[rank][0])
    print("Name:", top3[rank][1])
    print("Marks", top3[rank][2])
    print("Age", top3[rank][3])

    Output

    Enter a Number between 0 to 2: 1
    Rank: 2
    Name: Ravi
    Marks 987
    Age 17

    Wrapping Up!

    The " TypeError: list indices must be integers or slices, not float " is a common error. You will encounter it in your Python program when you use a float number instead of an integer as an index number. Converting a number into a float and using it as an index value is a common case when many Python developers encounter this error.

    if you use the dot operator when slicing a list rather than using a colon : to separate the start and end indexes, you will also get the same error.

    If you still get this error in your Python program, you can share your code and query in the comment section. We will try to help you with debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments