Python typeerror: list indices must be integers or slices, not str Solution

Posted in /  

Python typeerror: list indices must be integers or slices, not str Solution
vinaykhatri

Vinay Khatri
Last updated on December 22, 2024

    list indices must be integers or slices, not str is one of the most common Python typeerrors. A typeerror generally occurs when we mistake the property or functionality of an object or data type with some other object.

    In this Python guide, we will walk through this Python error and discuss the reason for its occurrence and solution with examples. So let's get started.

    The Problem: typeerror: list indices must be integers or slices, not str

    We will start with breaking the error statement and discussing what it is trying to tell us. The error statement " typeerror: list indices must be integers or slices, not str " contain two messages separated with a colon " : ".

    The typeerror is the error type that tells us we are performing some illegal operation on an object that it does not support. The list indices must be integers or slices, not str is the error message that specifies the actual error.

    What does "list indices must be integers or slices, not str" means?

    The first word of the error is list . This means the error is related to the Python list object. A Python list is a container of sequential data elements, and to access individual elements from the list, we can perform indexing. In indexing, we use the integer value inside the square bracket after the list variable name.

    Example

    mylist = [1,2,3,4]
    
    #access first element 
    print(mylist[0])

    Every value present in the list has a unique index number, and as the list stores all its elements in sequential order, the index number starts from 0 up to n-1 , where n is the total number of elements present in the list.

    Reason of error

    Now we know that the error belongs to the Python list, and the Python list stores its elements in sequential order, gives a unique index number to its elements, and all the index numbers are the integer value starting from 0 upto n-1.

    But if we mistreat the integer index value of a list with a string digit, the Python interpreter throws the  type error called list indices must be integers or slices, not str .

    Example

    # create a list
    my_list = [2,3,4,5,6,7,8]
    
    # string index
    index = "0"
    
    # error
    print(my_list[index])

    Output

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

    Break Error Output

    The error output shows that we have an error in line 7 , which means with the print(my_list[index]) statement. If we look at our code in line 7, there we are trying to print the first elements of the list my_list using the index value "0" .

    But the list supports only integer index values, but in our program, our index value is a string. That's why the Python interpreter threw the error. Now you know what actually the error message is trying to tell us. The message is very straightforward. It tells us that the passed index is not an integer or slice but a string value.

    Solution

    The solution for the error is pretty simple. We just need to look at the error code line and check if the index value is an integer or a string. We are receiving the error. This means the index value is a string, so now we need to change it to the integer value.

    Example

    # create a list
    my_list = [2,3,4,5,6,7,8]
    
    # integer index
    index = 0
    
    print(my_list[index]) # 2

    We could have also converted the string index value to the integer using the Python int function.

    Example

    # create a list
    my_list = [2,3,4,5,6,7,8]
    
    # integer index
    index = "0"
    
    print(my_list[int(index)]) #2

    Common Scenario

    The most common scenario when you receive this error is when you ask the user to input the index number, and you forget to convert the entered number into an integer.

    Example

    # input index number from the user
    index = input("Enter the index number: ")
    
    my_list = [23,454,52,34,235,56,34]
    
    print(my_list[index])

    output

    Enter the index number: 4
    Traceback (most recent call last):
    File "main.py", line 6, in <module>
    print(my_list[index])
    TypeError: list indices must be integers or slices, not str

    The input() function accepts the input from the user as a string object. But when we want to access the list elements, we need the integer index value. So to convert the string number to an integer number, we can use the int() function.

    Solution

    # input index number from the user
    index = int(input("Enter the index number: "))
    
    my_list = [23,454,52,34,235,56,34]
    
    print(my_list[index])

    Output

    Enter the index number: 4
    235

    The statement " int(input("Enter the index number: ")) " will convert the user entered string number into Python integer value.

    Conclusion

    In this tutorial, we discussed one of the most common Python errors, "typeerror: list indices must be integers or slices, not str".  This error occurs in Python when you pass the string object as an index value to the list index when you try to access the list elements. The Python list also supports slicing in that also we need to pass the integer index value. If you pass the string object, you will receive the same error.

    If you are getting this error and confused with your code, please comment down your code. We will try to solve it for you.

    People are also reading:

    Leave a Comment on this Post

    0 Comments