[Fixed] TypeError: list indices must be integers or slices, not tuple

When working with list elements in Python, you must reference them by their index position. Python will throw a “typeerror: list indices must be integers or slices, not tuple” if you specify a tuple or a list as an index. List indices must be integers or slices, not tuples.

Our discussion in this article will cover what this error means and how to correct the typeerror in your code.

TypeError: list indices must be integers or slices, not tuple Fix

Code Example 1 – 1D List

Let’s look at a code example to see how to reproduce this error, and then we’ll talk about how to fix it effectively.

# Python Accessing List
listItems = ["A", "B", "C"]

#Accessing List without index 
#and Printing It
print(listItems[1,2])

Output:

Traceback (most recent call last):
  File "c:\Users\Coduber\test.py", line 6, in <module>
    print(listItems[1,2])
TypeError: list indices must be integers or slices, not tuple
TypeError: list indices must be integers or slices, not tuple

In the preceding example, we are passing the index value [1,2] to the list element in order to access the list element. The comma in between will cause the Python interpreter to become confused, and it will treat it as a tuple, resulting in a typeerror: list indices must be integers or slices, not tuples.

Solution – Let us talk about a possible solution to the above-mentioned error, which we have reproduced.

Because the tuple value does not correspond to an index value in the list, we are unable to specify a tuple value to access an item from a list using it. To access a list, you must use an appropriate index, and instead of a comma, you must use a colon: as shown in the example below.

# Python Accessing List
listItems = ["A", "B", "C", "D", "E"]

#Accessing List without index 
#and Printing It
print(listItems[1:4])

Output:

['B', 'C', 'D']
TypeError: list indices must be integers or slices, not tuple

Code Example 2 – 2D List

It is also not uncommon for developers to encounter difficulties when attempting to construct a list within another list. The Python interpreter throws a TypeError here because there is no comma between the expressions for the outer list items.

# Python Accessing List
listItems = [["A", "B"] ["C", "D"] ['E']]

#Accessing List without index 
#and Printing It
print(listItems[0])

Output:

c:\Users\Coduber\test.py:2: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma?
  listItems = [["A", "B"] ["C", "D"] ['E']]
Traceback (most recent call last):
  File "c:\Users\Coduber\test.py", line 2, in <module>
    listItems = [["A", "B"] ["C", "D"] ['E']]
TypeError: list indices must be integers or slices, not tuple
TypeError: list indices must be integers or slices, not tuple

The solution to the above error:

The issue here is that we have forgotten to include the comma between each of our list elements once more. In order to solve this problem, we must use a comma to separate the lists in our list of lists, as shown in the example below.

# Python Accessing List
listItems = [["A", "B"], ["C", "D"], ['E']]

#Accessing List without index 
#and Printing It
print(listItems[0])

Output:

['A', 'B']

Wrap Up

I hope you understand why a typeerror: list indices must be integers or slices, not tuples error occurs and how to fix it in your program right away. I’ve listed two methods that can cause this error, along with a possible solution for each.

If you are still receiving this error, please let me know in the comments section and I will be happy to assist you as soon as possible. Also, please let me know if you know of a better solution than the one discussed above.

Further Read:

  1. How To Replace Characters In A String In Python
  2. [Fixed] Deprecationwarning: find_element_by_* commands are deprecated
  3. [Fixed] DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  4. Python Random Module – Generate Random Numbers/Sequences

1 thought on “[Fixed] TypeError: list indices must be integers or slices, not tuple”

Leave a Comment