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

If you come across an error like this: TypeError: list indices must be integers or slices, not str? Do not worry, it is just hinting that you have used the wrong data type (in this case, a string) as an index of a list item, instead of using an integer. Let me explain a little bit about what this means, then we will get into the details of how to fix it.

List Indices must be Integers or Slices, not str
List Indices must be Integers or Slices, not str

When Does the TypeError: list indices must be integers or slices, not str happen.

So, Each list item in a Python list has an index. An index is a number that represents the position of a list item in a list. It’s sort of like an identification number (ID). We usually use these index values to select a list item in a list or a sequence of list items from the list. The first list item in a list has an index of 0 (zero) and the next 1 and so on and so forth till the last item in the list.

Index0123
List Item'Steve'18{
'age': 18,
'name': 'Steve'
}
500

As you can see, all these indices are purely integers that return <class 'int'> when passed in the type() function. When we use any other data type other than an integer to select an item from a list, that’s when we get the error: TypeError: list indices must be integers or slices, not str. In this post, I will show you how to fix this error.

Check out my blog post on how to check if a list is empty in Python.

Then, you may also be asking, we have only covered integers as the only required datatype for selecting list items from a list, what about the ‘slices’ part that is also present in the error? Well, slices are also made up of integers and instead of selecting only one list item from the list; slices select a range of list items.

How to fix TypeError: list indices must be integers or slices, not str happen.

When the list indices must be integers or slices, not str error occurs, it mentions some valuable information we can use to quickly locate where the error has happened therefore quickly solving it. Let’s take a look at an example:

Traceback (most recent call last):
  ➊File "file.py", ➋line 5, in <module>
      ➌print(error_words[first_word])
TypeError: list indices must be integers or slices, not str

As you can tell, at ➊ Python mentions the file that the error has occurred, and at ➋ the line that is causing the error and below that at ➌, it even shows you the very line that is causing the error.

Using the information provided in the error itself, and the ways to solve the error that I will share with you in this post, you can quickly solve the list indices must be integers or slices, not str error. Let’s see how to do that.

The TypeError: list indices must be integers or slices, not str usually happens because of one of the following things.

1. Fix TypeError: list indices must be integers or slices, not str when string variables are left unconverted.

The most common occurrence of the list indices must be integers or slices, not str error usually happens when we try to index a list item with a string not an integer. But this also does not happen in a straightforward manner. It involves using a variable that holds a string value as a list item index. Let’s take a look at the example below.

error_words = ['list', 'indices', 'must', 'integers', 'slices', 'not', 'str']
first_word_index = '0'

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

The way to fix this TypeError: list indices must be integers or slices, not str when string variables are left unconverted is to convert the string that is holding the list index into an integer. We can do this programmatically by using the int() function. But manually, we can fix it just by removing the quotes from the string. Let’s take a look at the following example:

error_words = ['list', 'indices', 'must', 'integers', 'slices', 'not', 'str']
first_word_index = '0'
                  👇 #use int() to cast str to int
print(error_words[int(first_word_index)])

2. Fix TypeError: list indices must be integers or slices, not str when working with a list of Dictionaries.

The next popular occurrence of the list indices must be integers or slices, not str error occurs mostly to beginners when they are working with a list of dictionaries. It happens in the same fashion as the previous one in that a string value is used as a list item index. But in this case, the confusion comes from treating lists like dictionaries.

We know that a dictionary is made up of key-value pairs and we use the key – which is usually a string but can also be an integer – to reference a dictionary. When we have a list of dictionaries, each dictionary in that list is a list item so it has its own index. In order to select a key that is in a dictionary inside a list, we should start by referencing the list item index first, then we can refer to the key in that dictionary. Most people will skip this part, and when they do, they are likely to get the classic TypeError: list indices must be integers or slices, not str. Let’s take a look at an example:

Case 1: When referencing a dictionary that is in a list

actors = [
    {
        'name': 'Tom Cruise',
        'age': 60
    },
    {
        'name': 'Arnold Schwarzenegger',
        'age': 75 
    },
    {
        'name': 'Adam Sandler',
        'age': 56 
    },
]

if actors['name'] == 'Adam Sandler':
    print('He is a genius')
Traceback (most recent call last):
  File "main.py", line 16, in <module>
    if actors['name'] == 'Adam Sandler':
TypeError: list indices must be integers or slices, not str

I get the list indices must be integers or slices, not str here because I have tried to access a dictionary value using a key the same way I would have done with a list. But, which key-value pair am I trying to access here? Remember each dictionary in the actors list is a list item. Therefore, If I know which dictionary I want to select, I will use its index. Here is the solution.

if actors[2]['name'] == 'Adam Sandler':
    print('He is a genius')

Case 2: When looping over a list of dictionaries

Another mistake happens when looping over a list of dictionaries.

for actor in actors:
    name = actors['name']
    age = actors['age']
    print(f'{name} is {age} years old')
Traceback (most recent call last):
  File "main.py", line 17, in <module>
    name = actors['name']
TypeError: list indices must be integers or slices, not str

Solution

for actor in actors:
    name = actor['name']
    age = actor['age']
    print(f'{name} is {age} years old')

Conclusion: Fix List Indices must be Integers or Slices, not str

If you have come across an error like TypeError: list indices must be integers or slices, not str? Do not worry, it is just hinting that you have used the wrong data type as an index of a list item. Usually, a string when you are supposed to be using an integer. There are other related errors like list indices must be integers or slices, not tuple, list indices must be integers or slices not float, which can be solved in the same way we have done the one we were solving in this post. Always make sure you use the help of information provided in the error to check the file and the line where the error has occurred.

I’ll see you in other related posts. Peace!

Fix Python TypeError: List Indices must be Integers or Slices, not str
Scroll to top