Skip to main content

How to Resolve Python Error "TypeError: list indices must be integers or slices, not tuple"

The TypeError: list indices must be integers or slices, not tuple is a specific error in Python indicating that you attempted to access an element or slice of a list using square brackets [], but provided a tuple inside the brackets instead of the required integer index or slice object.

This guide explains the valid ways to index lists and provides clear solutions to fix this common indexing error.

Understanding Python List Indexing

Python lists are ordered sequences, and you access their elements using zero-based integer indices or slices within square brackets []:

  • Integer Index: my_list[i] retrieves the single element at index i (where i=0 for the first element, i=1 for the second, i=-1 for the last, etc.).
    my_list = ['a', 'b', 'c', 'd']
    print(my_list[0]) # Output: a
    print(my_list[2]) # Output: c
    print(my_list[-1]) # Output: d
  • Slice: my_list[start:stop:step] retrieves a new list containing a range of elements.
    my_list = ['a', 'b', 'c', 'd', 'e']
    print(my_list[1:4]) # Output: ['b', 'c', 'd'] (index 1 up to, not including, 4)
    print(my_list[:2]) # Output: ['a', 'b'] (from start up to index 2)
    print(my_list[3:]) # Output: ['d', 'e'] (from index 3 to the end)
note

The TypeError ... not tuple occurs because Python doesn't allow you to put a tuple directly inside the brackets for standard list indexing.

Cause 1: Incorrect Syntax for Indexing (e.g., my_list[0, 1])

This is the most direct cause – using a comma-separated sequence inside the square brackets. Python interprets 0, 1 inside [] as a tuple literal (0, 1).

Error Scenario

my_list = ['w', 'x', 'y', 'z']
nested_list = [['a', 'b'], ['c', 'd']]

try:
# ⛔️ TypeError: list indices must be integers or slices, not tuple
# Python sees [0, 1] as indexing with the tuple (0, 1)
element = my_list[0, 1]
print(element)
except TypeError as e:
print(f"Error accessing my_list: {e}")

try:
# ⛔️ TypeError: list indices must be integers or slices, not tuple
# Trying to access nested element with tuple index
nested_element = nested_list[0, 1]
print(nested_element)
except TypeError as e:
print(f"Error accessing nested_list: {e}")

Solution: Use Single Integer Indices

Access elements one at a time using their integer index.

my_list = ['w', 'x', 'y', 'z']
print(f"Element at index 0: {my_list[0]}") # Output: Element at index 0: w
print(f"Element at index 1: {my_list[1]}") # Output: Element at index 1: x

Solution: Use Slices for Ranges (start:stop)

Use the colon : for slicing to get a range of elements.

my_list = ['w', 'x', 'y', 'z']

# Get elements from index 0 up to (not including) 2
slice_0_to_2 = my_list[0:2]
print(f"Slice [0:2]: {slice_0_to_2}") # Output: Slice [0:2]: ['w', 'x']

Solution: Correct Nested List Access (my_list[0][1])

To access an element within a nested list (a list inside another list), use multiple sets of square brackets sequentially.

nested_list = [['a', 'b'], ['c', 'd']]

# ✅ Correct Nested Access:
# Access the first inner list (index 0), then the second element within THAT list (index 1)
element_0_1 = nested_list[0][1]
print(f"Element at [0][1]: '{element_0_1}'") # Output: Element at [0][1]: 'b'

element_1_0 = nested_list[1][0]
print(f"Element at [1][0]: '{element_1_0}'") # Output: Element at [1][0]: 'c'

Accessing Multiple, Non-Consecutive Indices

Standard Python lists do not support accessing multiple arbitrary indices directly like my_list[[0, 2]]. You need to access them individually or use techniques like list comprehensions if generating a new list.

my_list = ['w', 'x', 'y', 'z']
indices_to_get = [0, 3]

# ✅ Access individually
val0 = my_list[0]
val3 = my_list[3]
print(f"Values at 0 and 3: {val0}, {val3}") # Output: Values at 0 and 3: w, z

# ✅ Using list comprehension to create a new list
selected_elements = [my_list[i] for i in indices_to_get]
print(f"Selected elements: {selected_elements}") # Output: Selected elements: ['w', 'z']

Cause 2: Using a Tuple Variable as an Index

You might define a tuple variable and then mistakenly use that variable as the index for your list.

Error Scenario

my_list = [10, 20, 30, 40, 50]
index_pair = (1, 3) # This is a tuple

print(f"Type of index_pair: {type(index_pair)}") # Output: <class 'tuple'>

try:
# ⛔️ TypeError: list indices must be integers or slices, not tuple
# Passing the tuple variable as the index
elements = my_list[index_pair]
print(elements)
except TypeError as e:
print(e)

Solution: Use Integer Variables or Literals

Ensure that the value inside the square brackets resolves to a single integer or a slice object.

my_list = [10, 20, 30, 40, 50]
index_one = 1
index_three = 3

# ✅ Use integer variables
element1 = my_list[index_one]
element3 = my_list[index_three]
print(f"Elements at indices {index_one} & {index_three}: {element1}, {element3}")
# Output: Elements at indices 1 & 3: 20, 40

# ✅ Use integer literals directly
element1_lit = my_list[1]
element3_lit = my_list[3]
print(f"Elements at indices 1 & 3: {element1_lit}, {element3_lit}")
# Output: Elements at indices 1 & 3: 20, 40

Sometimes, a missing comma when defining a list of lists can lead to confusing errors, though usually a SyntaxError or a different TypeError rather than the specific "indices... not tuple" one.

# Missing comma between ['a','b'] and ['c','d']
try:
# ⛔️ Might cause SyntaxError or TypeError depending on context/version
incorrect_list = [['a', 'b']['c', 'd']]
except Exception as e:
print(f"Error from missing comma: {type(e).__name__}: {e}")

# ✅ Corrected list definition with comma
correct_list = [['a', 'b'], ['c', 'd']]
print(f"Correctly defined list: {correct_list}")

Output:

<main.py>:4: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma?
ERROR!
Error from missing comma: TypeError: list indices must be integers or slices, not tuple
Correctly defined list: [['a', 'b'], ['c', 'd']]
note

Always ensure list elements are separated by commas.

When Tuple Indexing is Used (NumPy Arrays)

It's important to note that libraries like NumPy do allow indexing multi-dimensional arrays using tuples. This is a feature of NumPy arrays, not standard Python lists.

# Note: Requires 'pip install numpy'
import numpy as np

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Convert to NumPy array
np_array = np.array(list_of_lists)

# ✅ NumPy allows tuple indexing for multi-dimensional access
# Get element at row 1, column 2 (which is 6)
element_1_2 = np_array[1, 2]
print(f"NumPy array:\n{np_array}")
print(f"Element at [1, 2]: {element_1_2}") # Output: Element at [1, 2]: 6

# Get first column (all rows, column 0)
first_column = np_array[:, 0]
print(f"First column: {first_column}") # Output: First column: [1 4 7]

# Get elements at specific indices (fancy indexing)
selected = np_array[[0, 2], [0, 1]] # Gets elements at (0,0) and (2,1) -> [1, 8]
print(f"Fancy indexing result: {selected}") # Output: Fancy indexing result: [1 8]

Output:

NumPy array:
[[1 2 3]
[4 5 6]
[7 8 9]]
Element at [1, 2]: 6
First column: [1 4 7]
Fancy indexing result: [1 8]

If you see code using my_array[0, 1], check if my_array is a NumPy array.

Debugging the Error (type())

When you encounter the TypeError: list indices must be integers or slices, not tuple:

  1. Look at the line number in the traceback.
  2. Examine the expression inside the square brackets [].
  3. If it's a literal like 0, 1, you know it's incorrect syntax.
  4. If it's a variable my_index, print its type: print(type(my_index)). This will confirm if it's <class 'tuple'> when it should be <class 'int'> or <class 'slice'>.

Conclusion

The TypeError: list indices must be integers or slices, not tuple occurs when you incorrectly try to index a standard Python list using a tuple within the square brackets ([]).

To fix this:

  • Ensure you use a single integer for accessing one element (e.g., my_list[0]).
  • Use slice notation (start:stop:step) for ranges (e.g., my_list[1:4]).
  • Access elements in nested lists using sequential brackets (e.g., my_list[0][1]).
  • Access multiple non-consecutive elements individually or using list comprehensions/loops.
  • If using a variable for indexing, make sure it holds an integer or a slice, not a tuple.
  • Remember that NumPy arrays do support tuple indexing, but standard Python lists do not.

By adhering to Python's list indexing rules, you can easily avoid this TypeError.