TypeError list indices must be integers or slices not str

TypeError: List Indices must be Integers or Slices not str – Python Fix Stepwise

If you are stuck in the python code with this error [ TypeError: list indices must be integers or slices, not str ]. You are not alone as a python developer most of us face this error every week. In this article, I will explain to you the reason for this error with its solution. So let’s start.

 

TypeError: list indices must be integers or slices not str ( Reason) –

The only reason for this error is when we put the string in list subscript while accessing the list’s element. Let’s see a quick demo.

list indices must be integers not str example
list indices must be integers not str example

In the above example, we can see very clearly that we have created a list ( sample_list) which contains four-elements. Now when we tried to access its element “A” it shows the same error. In the next section, we will see its solution.

TypeError: list indices must be integers or slices, not str ( Fix/Solution) –

We can only access the list element directly with its position. As you can we that position must be a single integer or range of integers. Let’s see how.

sample_list=["A","B","C","D"]
element=sample_list[0]
print(element)

Here is the output for the above sample code.

list indices must be integers not str Fix
list indices must be integers, not str Fix

 

A list in python can contain any type of object in sequential form. It can we int, str, dictionaries (dict), other objects, or nested list, etc. A typical list starts from 0 indices.

 

Case studies for Python error[TypeError: list indices must be integers or slices, not str]-

Case 1:

if we have a list of python dictionaries and we want to compare a particular key of the dictionary with some variable.  Let’s see the below code.

sample_list=[{"dict1_key1":1, "dict1_key2":2},
             {"dict2_key1":3, "dict2_key2":4}]

if sample_list["dict2_key1"]==3:
  print("Found the value : condition match")

The above code will through the below output.

TypeError                                 Traceback (most recent call last)
 in ()
      2              {"dict2_key1":3, "dict2_key2":4}]
      3 
----> 4 if sample_list["dict2_key1"]==3:
      5   print("Found the value : condition match")

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

Here we need to iterate the list and look for the key in the corresponding dictionary. Lets the correct implementation:

sample_list=[{"dict1_key1":1, "dict1_key2":2},
             {"dict2_key1":3, "dict2_key2":4}]

for ele in range(len(sample_list)):
  if sample_list[ele].get("dict2_key1")==3:
    print("Found the value : condition match")

Above, We have iterated the list with int value. Below is the output for the correct implementation of list indices.

Found the value : condition match

 

Case 2:

When the index/ position is an integer but their declaration is wrong. Let’s see the case with a coding example.

index in list is str
the index in the list is str

In the above example, we are accessing the list element with the index. But we have already declared the index as str (string object). The correct implementation is below.


sample_list=["A","B","C","D"]
index=2
print(sample_list[index])

I hope the above cases are more than sufficient to explain to you the fix and cause of this error.

Thanks
Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner