How to fix TypeError: list indices must be integers or slices, not tuple?

You see TypeError: list indices must be integers or slices, not tuple when you try to index the list with a tuple. Generally, the list indices are expected to be integers or slices(specified with the colon operator). This error shows up if:

  • You’re using a comma instead of a colon for slicing.
  • You miss a comma while defining a list.
  • You index a nested list with a comma.

Let’s discuss the causes and fixes for this error in detail with examples.

Also, check TypeError: only integer scalar arrays can be converted to a scalar index

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

Case 1: Using a comma instead of a colon for slicing.

Lets say we have a list [1,2,3,4,5,6,7]. To obtain the elements [2,3] from the list, we can use list slicing as shown below.

lst=[1,2,3,4,5,6,7,8]
print(lst[1,3])

Output:

Traceback (most recent call last):
  File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\Test.py", line 2, in <module>
    print(lst[1,3])
TypeError: list indices must be integers or slices, not tuple

When you pass values within the square brackets, the python interpreter treats it as an index. Python lists support indexing with integers and slices. We pass [1,3] in our program, this is treated as a tuple. Since indexing with tuples is not supported, you see TypeError: list indices must be integers or slices, not a tuple.

Fix :

To fix the error, use a colon(:) instead of a comma. [1:3] -> tells the interpreter to slice the list. This fixes the issue.

lst=[1,2,3,4,5,6,7,8]
print(lst[1:3])

Output:

[2, 3]

 

Case 2: Missing a comma while creating a nested list.

This mistake is common among beginners. If you miss a comma while creating a nested list, you will see the error. Note that there is no comma between the outer lists.

nested_list= [['A','B']
              ['C','D']
              ['E','F']]

Output:

C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\Test.py:1: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma?
  nested_list= [['A','B']
Traceback (most recent call last):
  File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\Test.py", line 1, in <module>
    nested_list= [['A','B']
TypeError: list indices must be integers or slices, not tuple

As you can notice, the python interpreter throws a Syntax warning first, asking if you missed a comma, and then throws the error.

Fix :

To fix this, add a comma to separate the list elements, as shown below.

nested_list= [['A','B'],
              ['C','D'],
              ['E','F']]

 

Case 3: Indexing a nested list with a comma.

In Numpy arrays, commas can be used to index the arrays. This is confused with lists. Many of them end up using a comma instead of two sets of square braces.

Let’s say you have a nested list [[‘A’, ‘B’],[‘C’, ‘D’]] and you want to access the list item B.

nested_list= [['A','B'],['C','D']]
print(nested_list[0,1])

Output:

Traceback (most recent call last):
  File "C:\Users\paian\PycharmProjects\PythonHowTo\venv\Solutions\Test.py", line 3, in <module>
    print(nested_list[0,1])
TypeError: list indices must be integers or slices, not tuple

When the Python interpreter sees [0,1], it treats this as a tuple. Since indexing the list with tuples is not allowed, you see the error.

Fix :

Multi-dimenisonal indexing in lists is to be done using list_name[outer_list_index][inner_list_index] format. So use, nested_list[0][1] to solve the error.

nested_list= [['A','B'],['C','D']]
print(nested_list[0][1])

Output:

B

Conclusion

This brings us to the end of this article. We hope this helped you solve the error. Kindly comment and let us know the fix that helped you. Thank you for Reading.

 

 

If you enjoyed reading, share this article.

Anusha Pai is a Software Engineer having a long experience in the IT industry and having a passion to write. She has a keen interest in writing Python Errorfixes, Solutions, and Tutorials.

1 thought on “How to fix TypeError: list indices must be integers or slices, not tuple?”

Leave a Comment