. Advertisement .
..3..
. Advertisement .
..4..
When incorporating Pythonlists in a script, you may see the “TypeError: list indices must be integers or slices, not str” message. This troubleshooting guide will show you how to get rid of this quickly.
TypeError: list indices must be integers or slices, not str
By printing this error message, Python is trying to tell you that it has been given the wrong index for accessing a list.
This is pretty straightforward: you are using strings while it is expecting integers or slices. They are the only ways to refer to specific elements in a Python list.
Each item in a list has a unique position, which Python uses to index it. You can use positive or negative indexing; it is your choice. But you must provide an integer value.
Example:
>>> sites = ['ITTutoria', 'Stack Overflow', 'Quora']
>>> sites[0]
'ITTutoria'
>>> sites[-3]
'ITTutoria'
Slicing is another option for accessing items in a Python list. Using a combination of indices, it can return multiple items at the same time.
Example:
>>> sites[0:2]
['ITTutoria', 'Stack Overflow']
If you accessing a list’s items with a str value, you will receive a TypeError exception (similar to when using a tuple):
>>> sites["0"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not str
How To Fix And Avoid This Error
While the mistake above can be easily detected in its simple form, it can be harder to do so in a real-world scenario. It might not be your intention to pass a string value as a list index, but sometimes your Python script does exactly that.
The most common source of this bug is the lack of understanding or caution with return types of methods and functions in Python. Here is an example to illustrate it:
meaning = ['certain',
'strongly believing something; having no doubts',
'that you can rely on to happen or to be true',
'used with a person's name to show that the speaker does not know the person']
selection = input('Which meaning of "certain" would you like to see (0, 1, or 2)? ')
print(meaning[selection])
Output:
Which meaning of "certain" would you like to see (0, 1, or 2)? 1
Traceback (most recent call last):
File "list_indices.py", line 10, in <module>
print(meaning[selection])
TypeError: list indices must be integers or slices, not str
At first glance, the script above should be able to run without a glitch. But after paying close attention to what Python documentation says about the function input(), you can see clearly what has gone wrong.
This function takes input from the user and converts it to a string before returning it. In the snippet above, the variable selection is assigned a string value, causing the error.
The quickest way to fix this is to convert input() ‘s return value to an int value before assigning it.
selection = int(input('Which meaning of "certain" would you like to see (0, 1, or 2)? '))
The program now should run as expected:
Which meaning of "certain" would you like to see (0, 1, or 2)? 1
strongly believing something; having no doubts
Another common misconception that can lead to this error happens when people iterate over a list with the for loop.
Example:
sites = ['ITTutoria', 'Stack Overflow', 'Quora']
for i in sites:
print(sites[i])
Output:
print(sites[i])
TypeError: list indices must be integers or slices, not str
When iterating a list, the variable after the for keyword (in this case, i) takes the value (which is a str value), not the index, of each item in the list. This should solve the problem:
sites = ['ITTutoria', 'Stack Overflow', 'Quora']
for i in sites:
print(i)
Output:
ITTutoria
Stack Overflow
Quora
Conclusion
The “TypeError: list indices must be integers or slices, not str” error in Python is the result of wrong index types while accessing lists. Make sure the indices are compatible with the operation, and your script will no longer have this bug.
Leave a comment