Python TypeError: list indices must be integers or slices, not str (Solution)

If you get the Python TypeError "list indices must be integers or slices, not str" it means you are trying to access elements on a list using strings.

 

Take this example:

 

items = ['test', 'test']

for i in items:
   items[i] = items[i].lower()

 

 

It won't work because the element values themselves are not the list indexes.

 

The solution is to get the length and create a range from it to iterate through like this:

 

items = ['test', 'test']

for i in range(len(items)):
   items[i] = items[i].lower()