TypeError: list indices must be integers or slices not tuple – debugging and fixes

Are you looking for a way to fix “TypeError: list indices must be integers or slices not tuple” error?

Python TypeError is the exception that indicates the invalid usage of an object type in an operation. The error usually occurs if an operation is performed on an object that does not support the operation itself, or an object of incorrect type. For example, TypeError is raised every time you try to concatenate an integer with a string.

In this article, we will dig deep into the “TypeError: list indices must be integers or slices not tuple” error and how beginners can debug and fix it quickly and effectively.

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

“TypeError: list indices must be integers or slices not tuple” indicates that you’re accessing a Python list using a tuple instead of an index or an index range. The “list indices” in the error message simply means “list indexes”.

Python beginners often mixed the slice syntax (using colons) with the list syntax (using commas).

Another common thing that cause this type of error is accessing a non-nested list using multi-dimensional slicing.

In order to fix the error, make sure you’re using the right syntax with the right object. Let’s see some examples:

# TypeError: list indices must be integers or slices, not tuple # because of missing comma a_list = [['a', 'b', 'c']['a1', 'b1', 'c1']]
Code language: Python (python)

In the example above, we forgot to add a comma when we’re trying to define a nested list, so Python mistakenly recognize it as we’re trying to access a list using indexes, hence display a TypeError.

Find the tuple indexes

There is a chance you may have used a tuple to access a list in a straightforward way, which triggers “TypeError: list indices must be integers or slices not tuple” error. Typically, such source code may look like below (oversimplified example).

a_list = ['a', 'b', 'c', 'd', 'e'] indexes = 0, 1 result = my_list[my_tuple]
Code language: Python (python)

In real life, the indexes may be the result of another operation, or the result from a function. In Python, typically a tuple is created by wrapping several objects in a parentheses pair (). For example: ('a', 'b') or ('a', 'b', 'c', 'd'). The parentheses are optional, which means 'a', 'b', 'c', 'd' also creates a tuple as well. In addition to that, you can also create a tuple using one of the following ways :

  • A pair of parentheses () creates an empty tuple
  • 0, or ('a',) also creates a tuple
  • Explicitly using tuple() constructor. For example, `tuple(‘a’, ‘b’).

With that many ways to create a tuple, follow the suggestions below to debug your program:

  • Use the type class to inspect the type of each object.
  • Alternatively, use isinstance to check whether the in object is an instance or a subclass of a class or not.

Check your syntax

In this case, simply inspect and add commas to the list definition accordingly.

# a_list definition with the proper syntax a_list = [['a', 'b', 'c'],['a1', 'b1', 'c1']]
Code language: Python (python)

Another common scenario is using an incorrect index accessor.

a_list = [['a', 'b', 'c'],['a1', 'b1', 'c1']] result = a_list[0, 0] # Will cause TypeError: list indices must be integers or slices, not tuple
Code language: Python (python)

In the example, we two integer separated by a comma to get an element from a nested list. This is the wrong syntax, which causes Python interpreter to mistakenly recognize it as a tuple.

If you’re trying to access an element from a nested list, you would have to use two pair of square brackets to do it. The first bracket pair will return the first element, then the second one will be the index to the element you just got.

a_list = [['a', 'b', 'c'],['a1', 'b1', 'c1']] result = a_list[0][0] # "result" will be 'a' # This equals to result1 = a_list[0] # Returns ['a', 'b', 'c'] result2 = result1[0] # Returns 'a'
Code language: Python (python)

In case you want to get a part of the list in Python, use the slicing syntax as follows:

a_list = ['a', 'b', 'c', 'd', 'e', 'f'] print(a_list[0:3]) # returns ['a', 'b', 'c'] print(a_list[:3]) # returns ['a', 'b', 'c'] print(a_list[3:]) # returns ['d', 'e', 'f']
Code language: Python (python)

Here we use the colon to separate the start and end indexes. Both the start and end index are optional. If not specified, the value for start index would be 0 and end index would be defaulted to the length of the list.

How to access multiple, unrelated list items without slicing? Simple, just use an index to access them separately and concatenate them afterwards.

a_list = ['a', 'b', 'c', 'd', 'e', 'f'] print([a_list[0], a_list[2], a_list[4]]) # returns ['a', 'c', 'e']
Code language: Python (python)

The code doesn’t look very elegant, but it does the job without having to use an external library. Another way to do it is using a for loop like below :

a_list = ['a', 'b', 'c', 'd', 'e', 'f'] indexes = [0,2,4] result = [a_list[x] for x in indexes] print(result) # returns ['a', 'c', 'e']
Code language: Python (python)

We hope that the article helped you successfully debug and fix “TypeError: list indices must be integers or slices not tuple” error in Python, as well as avoid encountering it in the future. We’ve also written a few other guides for fixing common Python errors, such as Fix “Max retries exceeded with URL” ,Python Unresolved Import in VSCode or “IndexError: List Index Out of Range” in Python.
If you have any suggestion, please feel free to leave a comment below.

Leave a Comment