Estimated reading time: 3 minutes
When working with Python lists in your data analytics projects, when you trying to read the data, a common problem occurs if you have a list of lists, and it is not properly formatted.
In this instance, Python will not be able to read one or more lists and as a result, will throw this error.
In order to understand how this problem occurs, we need to understand how to create a list of lists.
How to create a lists of lists
Let’s look at a simple list:
a = [1,2,3]
print(a)
print(type(a))
Result:
[1, 2, 3]
<class 'list'>
Let’s create a second list called b:
b = [4,5,6]
print(b)
print(type(b))
Result:
[4, 5, 6]
<class 'list'>
So if we want to join the lists together into one list ( hence a list of lists) then:
a = [1,2,3] b = [4,5,6] list_of_lists = [] list_of_lists.append(a) list_of_lists.append(b) print(list_of_lists) print(type(list_of_lists)) Result: [[1, 2, 3], [4, 5, 6]] <class 'list'>
So as can be seen the two lists are contained within a master list called “list_of_lists”.
So why does the error list indices must be integers or slices, not tuple occur?
Reason 1 ( Missing commas between lists)
If you manually type them in and forget the comma between the lists this will cause your problem:
a=[[1,2,3][4,5,6]] print(a) Result (Error): Traceback (most recent call last): line 10, in <module> a=[[1,2,3][4,5,6]] TypeError: list indices must be integers or slices, not tuple
But if you put a comma between the two lists then it returns no error:
a=[[1,2,3],[4,5,6]] print(a) Result (no error): [[1, 2, 3], [4, 5, 6]] Process finished with exit code 0
Reason 2 ( Comma in the wrong place)
Sometimes you have a list, and you only want to bring back some elements of the list, but not others:
In the below, we are trying to bring back the first two elements of the list.
a= [1,2,3,4,5] print(a[0:,2]) Result: Traceback (most recent call last): line 14, in <module> print(a[0:,2]) TypeError: list indices must be integers or slices, not tuple
The reason that the same error happens is the additional comma in a[0:,2], causes the error to appear as Python does not know how to process it.
This is easily fixed by removing the additional comma as follows:
a= [1,2,3,4,5] print(a[0:2]) Result: [1, 2] Process finished with exit code 0
So why is there a mention of tuples in the error output?
The final piece of the jigsaw needs to understand why there is a reference to a tuple in the error output?
If we return to a looking at a list of lists and look at their index values:
a=[[1,2,3],[4,5,6]]
z = [index for index, value in enumerate(a)]
print(z)
Result:
[0, 1]
Process finished with exit code 0
As can be seen, the index values are 0,1, which is correct.
As before removing the comma gives the error we are trying to solve for:
a=[[1,2,3][4,5,6]] z = [index for index, value in enumerate(a)] print(z) Result: Traceback (most recent call last): line 16, in <module> a=[[1,2,3][4,5,6]] TypeError: list indices must be integers or slices, not tuple
BUT the reference to the tuple comes from the fact that when you pass two arguments (say an index value) a Tuple is created, but in this instance, as the comma is missing the tuple is not created and the error is called.
This stems from the __getitem__
for a list built-in class cannot deal with tuple arguments that are not integers ( i.e. 0,1 like we have returned here) , so the error is thrown, it is looking for the two index values to pass into a tuple.