Python Lists use indexing to store its element in sequential order. In indexing, the list provides a unique sequential integer value to every element, and the index number starts from 0 up to n-1, where n is the total number of elements present in the list.
To access the individual element from a list, we can use the element index number inside the square bracket preceded by the list name. But if we specify a tuple object instead of an index value to access a list element, we will receive the
TypeError: list indices must be integers or slices, not tuple
Error.
In this Python debugging tutorial, we will learn what is
TypeError: list indices must be integers or slices, not tuple
Error in Python and how to solve it. We will also have a look at a common scenario example where most Python learners commit this mistake.
Python Error: TypeError: list indices must be integers , not tuple
The Python error
TypeError: list indices must be integers, not tuple
is divided into two statements Error Type and Error Message.
-
Error Type (
TypeError
): TypeError occurs in Python when we perform an incorrect operation of a Python object type. -
Error Message (
list indices must be integers or slices, not tuple
): This error message is telling us that we are using a tuple object instead of a valid index value.
Error Reason
The reason for this error is quite obvious, if you look at the error message, you can clearly tell why this error occurred in your program. Python list index value is always an integer value, even in list slicing, we use index integer value separated with colons.
But if we pass a tuple or values separated by commas as an index value, we will receive the
list indices must be integers or slices, not tuple
Error.
Example
my_list =['a', 'b', 'c', 'd', 'e', 'f']
# access list first element
print(my_list[0,])
Output
Traceback (most recent call last):
File "main.py", line 4, in <module>
print(my_list[0,])
TypeError: list indices must be integers or slices, not tuple
Break the code
We are receiving the error in our above program because at line 4, we have passed a tuple as an index value to access the first element of
my_list
object.
The Python interpreter read the comma-separated values as a tuple. That's why in line 4, where we are printing the
my_list
first, the element using the index value
0,
.
Python treated the
0,
statement as a tuple and threw the error because the index value must be an integer, not a tuple.
Solution
To solve the above program, we just need to remove the comma after
0
and it will be treated as an integer object.
my_list =['a', 'b', 'c', 'd', 'e', 'f']
# access list first element
print(my_list[0])
Output
a
A Common Scenario
The most common scenario where many Python learners encounter this error is when they use commas
,
by mistake for list slicing instead of a colon
:
.
Example Let's say we want to access the first four elements from our list, and for that list, slicing would be a perfect choice. Using list slicing, we can access a sequential part of the list using a single statement.
my_list =['a', 'b', 'c', 'd', 'e', 'f']
# access first 3 elements
print(my_list[0,4])
Output
Traceback (most recent call last):
File "main.py", line 4, in <module>
print(my_list[0,4])
TypeError: list indices must be integers or slices, not tuple
Break the code
In the above example, we tried to perform Python list slicing on our list object
my_list
to access its first 3 elements. But in line 4, instead of a colon
:
we used commas to specify the start
0
and end
4
indices for the list slicing.
Python interpreter read the
1,4
statement as a tuple and return the TypeError
list indices must be integers or slices, not tuple
.
Solution
The solution for the above problem is very simple. All we need to do is follow the correct Python list slicing syntax that is as follows:
list_name[start_index : end_index : steps]
Example
my_list =['a', 'b', 'c', 'd', 'e', 'f']
# access first 3 elements
print(my_list[0:4])
Output
['a', 'b', 'c', 'd']
Final Thoughts!
In this Python tutorial, we learned about
TypeError: list indices must be integers or slices, not tuple
Error and how to solve it. This error raises in Python when we use a tuple object instead of the integer index value to access an element from a Python list.
To solve this problem, you need to make sure that the error list element you are using must be accessed through a proper index value, not a tuple.
If you are still getting this error in your python program, you can share your code in the comment section with the query, and we will help you to debug it.
People are also reading:
- Online Python Compiler
- Python ValueError: invalid literal for int() with base 10: Solution
- Python XML Parser Tutorial
- Python TypeError: ‘float’ object is not callable Solution
- np.arange() | NumPy Arange Function in Python
- Python indexerror: list assignment index out of range Solution
- Python Internet Access
- Python typeerror: ‘list’ object is not callable Solution
- Python String count()
- Python TypeError: can only concatenate str (not “int”) to str Solution
Leave a Comment on this Post