Fix Python Error “List Indices Must Be Integers or Slices, Not Tuple”

One common error you might see while writing Python code is “list indices must be integers or slices, not tuple”. This tutorial will show you how to fix it.

The Python error “list indices must be integers or slices, not tuple” can occur in multiple scenarios. The simplest way to understand what’s causing an error is to analyze the error message. By looking at the error message, you can see that this error occurs when you pass a tuple as a list index instead of an integer or a slice.

Let’s fix this error together!

Example Scenario 1: Accidentally Using a Tuple as Index For Python List

Cause

The first reason you can see the Python error "list indices must be integers or slices, not tuple" is if you pass a tuple as the index of a list to try to access one of the list elements.

When you access an element of a list you have to pass a numeric integer to get a single element from the list or a slice to get multiple elements from the list.

Below you can see an example of code where by mistake we accidentally use an incorrect expression to access elements of a list.

numbers = [1, 2, 3, 4, 5]
print(numbers[0, 1])

You can see that we are passing [0, 1] to access elements of the list numbers and this is treated as a tuple by Python.

Here is the error you see when you execute this code:

Traceback (most recent call last):
  File "/opt/codefathertech/tutorials/list_indices_must_be_integers_or_slices_not_tuple.py", line 2, in <module>
    print(numbers[0, 1])
TypeError: list indices must be integers or slices, not tuple

Here, Python doesn’t know how to interpret [0, 1] as an index for the list.

Solution 1: Use a Valid Index or Slice

The way you solve the error we have seen above is to either pass a valid index if you want to access a single element of the list or a valid slice to access a subset of elements from the list.

Using an integer as an index

numbers = [1, 2, 3, 4, 5]
print(numbers[0])

# 1

By passing the index 0 as the index we get back the first item of the list numbers considering that indexing starts with zero.

Using slicing

numbers = [1, 2, 3, 4, 5]
print(numbers[0:2])

# [1, 2]

This slice returns the first and second elements of the Python list.

Example Scenario 2: Forgetting Commas to Separate Sublists in Nested List

Cause

The error “list indices must be integers or slices, not tuple” can also occur if you create a nested list in Python (also known as a list of lists) and you forget the comma that separates two sublists.

Let me explain to you what I mean…

When you create a nested list you have to separate sublists using commas.

For example, this is an example of a nested list that represents a matrix:

matrix = [[1, 2], [3, 4], [5, 6]]

This nested list contains three sublists. Each sublist is separated from the next one using a comma.

Now, imagine you accidentally create the nested list matrix in the following way:

matrix = [[1, 2], [3, 4] [5, 6]]

When you execute this code here is what happens:

/opt/codefathertech/tutorials/list_indices_must_be_integers_or_slices_not_tuple.py:1: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma?
  matrix = [[1, 2], [3, 4] [5, 6]]
Traceback (most recent call last):
  File "/opt/codefathertech/tutorials/list_indices_must_be_integers_or_slices_not_tuple.py", line 1, in <module>
    matrix = [[1, 2], [3, 4] [5, 6]]
TypeError: list indices must be integers or slices, not tuple

Python raises the TypeError “list indices must be integers or slices, not tuple”. Before the error on the last line, it also suggests that there might be a missing comma.

In this example, the error “list indices must be integers or slices, not tuple” occurs because of the missing comma between the second and third sublists of the nested list.

Notice the difference between the first and second sublists that are separated by a comma and the second and third sublists that are not separated by a comma.

Let’s look at the second and third sublists:

[3, 4] [5, 6]

The reason for the error is that Python thinks that [5, 6] is the index for the list [3, 4]. That’s why it raises the error “list indices must be integers or slices, not tuple”.

Solution 2: Use Comma Between Sublists in a Nested List

Let’s start from the nested list that was causing the TypeError we have seen before:

matrix = [[1, 2], [3, 4] [5, 6]]

Now, add a comma between the second and third sublists.

matrix = [[1, 2], [3, 4], [5, 6]]

This time Python won’t raise any error because the syntax used to create the nested list is correct.

Conclusion

Errors like “list indices must be integers or slices, not tuple” can be difficult to understand when you see them for the first time while writing and testing your code.

Also, the more complex the code is the less easy is to understand which part of the code is causing the error. Luckily Python helps you by providing the line number at which the error occurs.

Learning how to troubleshoot and fix this error will give you a deeper understanding of Python’s fundamentals.

Next time you see this error you will know exactly what to do!

Related article: Bring your knowledge about nested lists to the next level by going through the CodeFatherTech tutorial about Python nested lists.

Leave a Comment