The Python Typeerror: list indices must be integers or slices, not list
occurs when you try to use a list as an index of another list instead of using an integer or slice. To fix it, provide an integer or slice as the index instead of a list.
The list indices must be integers or slices, not list
error is a bit tricky to identify and fix, especially for Python beginners, so in this post I’ll show you some cases in which this error usually occur.
Example of list indices must be integers or slices, not list
error
As I mentioned above, this error occurs when you use a list as an index of another list. Take a look at the code below:
list1 = [1, 2, 3]
index_list = [0, 1, 2]
print(list1[index_list])
In the above example, we use the list index_list as an index of list1. However, index_list is neither an integer nor a slice, therefore, when you run the code, you are going to get the list indices must be integers or slices, not list
error.
To fix this error, you need to provide an integer or slice as the index. For example:
list1 = [1, 2, 3]
index_list = [0, 1, 2]
print(list1[index_list[0]]) # output: 1
In the above fixed example, we use index_list[0]
, which evaluates to an integer, as the index to list1
. This will print out the first element of list1
, which is 1
.
Most Common Case for list indices must be integers or slices, not list
error
For the example, I used a simple one to make you understand. However, this error does not always happen that simply. In this section, I’ll show you the most common scenario that usually result in the list indices must be integers or slices, not list
error.
The error usually happens when dealing with lists of lists and loops. Let’s take a look at an example:
a = [[1,2,3], [4,5,6]]
b = [[7,8,9], [10,11,12]]
list_a = [a,b]
for i in list:
for j in i:
print(list_a[j]) π
When you run the above code, you’ll get the error list indices must be integers or slices, not
, specifically pointing on the line where we have print(list_a[j])
. Why? Because the expression will result in a list.
How To Fix the Typeerror: list indices must be integers or slices, not list
will be discussed in the next section.
How To Fix the Typeerror: list indices must be integers or slices, not list
To fix the Typeerror: list indices must be integers or slices, not list
, you have to provide an integer or slice as the index instead of a list. When you get this error, it means that there is somewhere in your code you’re doing this mistake. So look around, identify the mistake and fix it.
Let us look at a possible solution:
a = [[1,2,3], [4,5,6]]
b = [[7,8,9], [10,11,12]]
list_a = [a,b]
for i in list_a:
for j in i:
print(j)
Depending on what you want to achieve, this could be a viable solution.
However, if you’re not getting what you expected, perhaps you may want to use a different data structure to achieve what you want, like a dictionary.
This error is different from the list indices must be integers or slices, not str error in that instead of providing a string as index, in this case, you’d be providing a list.
I hope you were able to fix your error. If you have any more questions, please let me know in the comments section below. Good Luck!