TypeError: list indices must be integers or slices not tuple

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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

The Python "TypeError: list indices must be integers or slices, not tuple" occurs when we pass a tuple between the square brackets when accessing a list at index. To solve the error, make sure to separate nested list elements with commas and correct the index accessor.

typeerror list indices must be integers or slices not tuple

Here is one example of how the error occurs.

main.py
# ⛔️ TypeError: list indices must be integers or slices, not tuple my_list = [['a', 'b']['c', 'd']] # 👈️ forgot comma between elements

We forgot to separate the items in a two-dimensional list with a comma.

main.py
# ✅ works my_list = [['a', 'b'], ['c', 'd']]

You might also get the error if you use an incorrect index accessor.

main.py
my_list = [['a', 'b'], ['c', 'd']] # ⛔️ TypeError: list indices must be integers or slices, not tuple result = my_list[0, 0]
We have to use an integer (e.g. my_list[2]) or a slice (e.g. my_list[0:2]) for list indices.

If trying to get an element in a nested list, access the nested list using square brackets, and use square brackets again to access the specific element.

main.py
my_list = [['a', 'b'], ['c', 'd']] print(my_list[0][0]) # 👉️ 'a' print(my_list[0][1]) # 👉️ 'b'

We access the first list (index 0) with the first set of square brackets and then access the nested list at a specific index.

If you need to get a slice of a list, use a colon to separate the start and end indices.

main.py
my_list = ['a', 'b', 'c', 'd', 'e'] print(my_list[0:3]) # 👉️ ['a', 'b', 'c'] print(my_list[3:]) # 👉️ ['d', 'e']

The start index is inclusive, whereas the end index is exclusive (up to, but not including).

If you need to access multiple, unrelated list items, access them separately.

main.py
my_list = ['a', 'b', 'c', 'd', 'e'] first = my_list[0] print(first) # 👉️ 'a' second = my_list[1] print(second) # 👉️ 'b'

Make sure you aren't assigning a tuple to a variable by mistake.

main.py
my_list = ['a', 'b', 'c', 'd', 'e'] my_tuple = 0, 1 # ⛔️ TypeError: list indices must be integers or slices, not tuple result = my_list[my_tuple]

In case you declared a tuple by mistake, tuples are constructed in multiple ways:

  • Using a pair of parentheses () creates an empty tuple
  • Using a trailing comma - a, or (a,)
  • Separating items with commas - a, b or (a, b)
  • Using the tuple() constructor

If you use numpy and are trying to access a nested list with two indices, make sure to convert the list to a numpy array first.

main.py
import numpy as np my_list = [['a', 'b'], ['c', 'd'], ['e', 'f']] arr = np.array(my_list) result = arr[:, 0] print(result) # 👉️ ['a', 'c', 'e']

The example shows how to get the first item from each nested list in the array.

If you aren't sure what type of object a variable stores, use the type() class.

main.py
my_list = ['a', 'b', 'c', 'd', 'e'] print(type(my_list)) # 👉️ <class 'list'> print(isinstance(my_list, list)) # 👉️ True my_tuple = 0, 1 print(type(my_tuple)) # 👉️ <class 'tuple'> print(isinstance(my_tuple, tuple)) # 👉️ True

The type class returns the type of an object.

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

Conclusion #

The Python "TypeError: list indices must be integers or slices, not tuple" occurs when we pass a tuple between the square brackets when accessing a list at index. To solve the error, make sure to separate nested list elements with commas and correct the index accessor.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.