TypeError: list indices must be integers or slices, not str in Python

To solve TypeError: list indices must be integers or slices, not str, use the int() function to typecast the input into an int so that the index can be accessed. The int() is a built-in Python function that returns an integer from a given object or converts an integer in a given base to a decimal.

How TypeError: list indices must be integers or slices, not str is raised

TypeError is one of the standard exceptions in Python that is raised whenever an operation is performed on an incorrect/unsupported object type. This is a standard error in Python while accessing lists or any other series data type. Lists are indexed using numbers starting from 0. We can refer to the element by using the index of the list.

Let’s now see how the list is indexed:

Values: 5 7 9 11 13 17 19 77
Index: 0 1 2 3 4 5 6 7

 

Here, you can see that the index starts at 0 and ends at 7. This is because the length of the list is 8. 

Now let us see an example of how to access an element in a list:

Example

lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
n = input("Enter the index of the element you want: ")
print(lst[n])

Output

Enter the index of the element you want: 3

TypeError: list indices must be integers or slices, not str

At first look, this program looks right. We are getting an input index from the user, and let us consider that we input index as 3. In the following line, the element at index 3 will be displayed as 4. But this won’t happen because this program will raise an error : TypeError: list indices must be integers or slices, not str.

The main reason we got the TypeError is that when we get the input from the user, it will be in the string data type, and we want to input the index as an integer data type.

How to solve TypeError in Python

To solve the TypeError problem, convert the string to an integer using the int() function.

lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
n = int(input("Enter the index of the element you want: "))
print(lst[n])

Output

Enter the index of the element you want: 3
4

The TypeError is resolved now, and we get the output as expected.

Solve TypeError in Python dictionary

We can access all the values by a string key in dictionaries, but we cannot do that in other series data types. Let us see one more example of this kind of TypeError.

dict = [
 {"Name": "Ram"},
 {"Name": "Sam"},
 {"Name": "Raj"}
]
if(dict["Name"] == "Ram"):
  print("True")

Output

TypeError: list indices must be integers or slices, not str

The above program throws the TypeError because we used a list as a dictionary, but lists will always be lists right even though we use the list as a dictionary. So, it cannot inherit the properties of the dictionary. So this type of error is raised. Let us see how to solve this error.

dict = [
  {"Name": "Ram"},
  {"Name": "Sam"},
  {"Name": "Raj"}
]
for i in dict:
  if(i["Name"] == "Ram"):
    print("True")

Output

True

Let’s see another example.

a = ["Orange", "Mango", "Pine apple", "Apple"]
for fruit in a:
  if(a[fruit] == "Orange"):
    print("We have orange")

Output

TypeError: list indices must be integers or slices, not str

In this case, we used a string as an index. So here is the solution for that.

a = ["Orange", "Mango", "Pine apple", "Apple"]

for fruit in a:
   if(fruit == "Orange"):
      print("We have orange")

Output

We have orange

By using a for loop for iteration,  we can solve the TypeError. The TypeError is mainly caused when we call the list elements using string index. List elements can only be called using the integer index. Therefore, we should not use a string index for accessing elements in the list. At the same time, we can use string indexing in Dictionaries.

That’s it for this tutorial.

See also

ZeroDivisionError: division by zero in Python

Only size-1 arrays can be converted to Python scalars

Leave a Comment