Last modified: Apr 28, 2026 By Alexander Williams
Fix List Indices Must Be Integers or Slices
Python is a powerful language. But beginners often hit a confusing error: TypeError: list indices must be integers or slices, not tuple. This error stops your code. It tells you that you tried to access a list using a tuple instead of a number or slice.
Don't worry. This article explains the error in simple terms. You will learn why it happens and how to fix it. We will use short examples and clear code. Let's dive in.
What Does This Error Mean?
A Python list is an ordered collection. To get an item from a list, you use an index. An index must be an integer (like 0, 1, 2) or a slice (like 0:2). A tuple is a different data type. It looks like this: (1, 2). When you use a tuple as an index, Python gets confused. It raises the error.
For example, if you have a list my_list = [10, 20, 30] and you try my_list[(0, 1)], Python throws the error. It expects an integer or slice, not a tuple.
Common Causes of This Error
Let's look at three main reasons this error appears. Each cause has a simple fix.
1. Using a Tuple Instead of a Comma
In Python, parentheses () create tuples. A common mistake is using parentheses for multiple indices. For example, you might write my_list[(0, 1)] when you meant my_list[0, 1]. But (0, 1) is a tuple. Python sees it as a tuple index, which is invalid.
Fix: Use a comma without parentheses for multiple indices in a nested list. Or use separate lines.
# Wrong: Using tuple as index
my_list = [[1, 2], [3, 4]]
# This raises TypeError:
# print(my_list[(0, 1)])
# Correct: Use separate indices
print(my_list[0][1]) # Output: 2
2. Forgetting to Use Square Brackets for Indexing
Sometimes you accidentally use parentheses for indexing. Python interprets my_list(0) as a function call. That gives a different error. But if you write my_list[(0)], Python sees (0) as a tuple (though a single-element tuple is valid). It still works for a single element, but if you have two elements like (0, 1), it fails.
Fix: Always use square brackets [] for list indexing. Never use parentheses.
# Wrong: Using parentheses for indexing
my_list = [10, 20, 30]
# This raises error:
# print(my_list(0))
# Correct: Use square brackets
print(my_list[0]) # Output: 10
3. Using Multi-dimensional List Incorrectly
If you have a list of lists, you must index each level separately. A common mistake is writing my_2d_list[0, 1] instead of my_2d_list[0][1]. The first form passes a tuple (0, 1) to the outer list, causing the error.
Fix: Use consecutive square brackets for each dimension.
# Wrong: Using tuple for multi-dimensional access
matrix = [[1, 2], [3, 4]]
# This raises TypeError:
# print(matrix[0, 1])
# Correct: Use nested brackets
print(matrix[0][1]) # Output: 2
Real-World Example with Output
Let's see a full example. Suppose you have a list of student scores. You want the score of student 0 in test 1. Here's the wrong and right way.
# Wrong code that raises error
scores = [[85, 90], [78, 88], [92, 95]]
# Trying to get first student's second test score
# This raises TypeError:
# print(scores[0, 1])
# Correct code
print(scores[0][1]) # Output: 90
90
The error happens because scores[0, 1] is interpreted as scores[(0, 1)]. Python sees a tuple and complains. The fix is simple: use scores[0][1].
How to Debug This Error
When you see this error, check these things:
1. Look at the index. Is it a tuple? Print the index variable to see its type. Use type(index) to confirm.
2. Check your brackets. Did you use parentheses () instead of square brackets []?
3. Review multi-dimensional access. For nested lists, use list[i][j] not list[i, j].
4. Use a debugger or print statements. Print the index and the list to see what you are passing.
# Debugging example
my_list = [1, 2, 3]
index = (0, 1) # This is a tuple
print(type(index)) # Output:
# This will raise error:
# print(my_list[index])
Once you see it's a tuple, you know the fix. Change it to an integer or slice.
Preventing the Error in the Future
Good habits help you avoid this error. Here are tips:
Use clear variable names. Name your indices like row and col for clarity.
Use list comprehension or loops. They avoid manual indexing mistakes.
Test small code snippets. Run a small example before writing larger code.
Learn about Python's enumerate function. It gives you integer indices automatically.
For more on handling data types correctly, check our Python Character Encoding Guide for Beginners to avoid similar type errors.
Conclusion
The TypeError: list indices must be integers or slices, not tuple is a common beginner mistake. It happens when you use a tuple instead of an integer or slice for list indexing. The fix is simple: use square brackets and separate indices for nested lists. Always check your index type with type(). With practice, you will avoid this error. Keep coding and learning.
If you found this helpful, explore more Python debugging tips. Happy coding!