Register Login

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

Updated Jan 17, 2020

list indices must be integers or slices, not tuple

Elements in a Python list are accessible using their list indices. You have to mention the name of the list and the index. But this index has to be an integer. If you incorrectly put a tuple or a list as the index, it will result in an error. You are likely to encounter an error called typeerror list indices must be integers or slices, not tuple.

The only way to resolve this situation is to pass in an integer in a slice as the indices while performing any operation using lists. We will delve deeper into this problem in this article.

Example 1

# Python 3 Code
numbers=[1,2,3,4]
print(numbers[0:,3])

Output

Traceback (most recent call last):
File "file-line.py", line 2, in <module>
print(numbers[0:,3])
TypeError: list indices must be integers or slices, not tuple

Here, the TypeError is encountered in the second line as the list indices are not coded properly. In the second line, you must not put a comma within the square brackets. The comma confuses Python as it seems to be a tuple - when it is expecting an integer for the index value.

TypeError list indices must be integers or slices, not tuple

The solution to this problem is as follows:

# Python 3 Code
numbers=[1,2,3,4]
print(numbers[0:3])

Output:

[1,2,3]

 


×