Python error: list indices must be integers or slices, not a tuple

When working with python, you will have to deal with multiple errors, and list indices must be integers or slices; not tuple is one of those obstinate errors which are hard to deal with. The beginners might feel overwhelmed with the number of errors they get while executing their first few codes. Well, that is not a big issue; the python programming language is very easy to learn, even for beginner programmers. It has easy-to-understand and straightforward syntax that makes it easy to understand for everyone.

In this article, we will discuss one of the most common errors the python developers get while writing and executing the codes: ‘python error: list indices must be integers or slices, not a tuple.’ Here, we will give you the solution to the error, and with the help of examples, we will help you get rid of the error.

A list is used to store many items or collection of multiple data in just one variable in a python program. The list in a python program can be of any data type, i.e., int, float, string, etc. The python list is created using square brackets ‘[].’ It could be an ordered list or an unordered list. In the ordered list, the order of the list items cannot be changed; they will remain the same. A new item, whenever added, is embedded at the end of the list.

However, some available methods can be used to change the order of the items in the list, but in general, it remains the same. The changeable list can easily be modified; new items can be added, old items can be removed, and any other modification can be performed easily. The list items can be rearranged, changed, or duplicated.

Moreover, the items in the list are indexed, [0] represents the first item in the list, while [1] represents the second item of the list and [n] represents the n+1 item, and so on. Since the lists are indexed, the items can be repeated with different index numbers. In other words, different indexes can have the same value. As the list’s index numbers, you can easily access an item anywhere in the list by calling the index number. However, if you refer to a tuple as a list index number, you will get the ‘python error: list indices must be integers or slices, not a tuple.’ A list item cannot be referred to as a tuple because a tuple does not correspond to an index item of the list. You will get the corresponding ‘tuple’ error mentioned above if you do so.

The beginners or efficient developers might get confused with tuples and lists and get caught with errors. In this article, you will learn about the earlier mentioned error and how to solve it with the help of examples.

Python error: list indices must be integers or slices, not a tuple

As discussed above, the list items are indexed using the numbers, i.e., [0], [1], [2], and so on. 0 refers to the first, 1 refers to the second, 2 refers to the third item, and so on. For example:

student_names = ['Daniyal,' 'Andree,' 'Taylor,' 'Sabrina,' 'John']

This list consists of five items. ‘Daniyal’ is stored on index [0], ‘Andree’ is stored on index [1], and so on. When you need to access an item from the list, you can easily refer to the required item of the list with its index; for example,

student_names[3] will return “Sabrina.”

Here is the output.

However, if you try to refer to the list items as a tuple, you will get the following error:

python error: list indices must be integers or slices, not a tuple. See the example below:

Below you can view the result.

Text Description automatically generated

As you can see, in this example, the arguments are passed as tuple [0,3], which is not the correct way to refer to an index of a list. The python interpreters will get confused due to the comma between the two indexes and will through the python error: list indices must be integers or slices, not a tuple. The solution to getting rid of this error is to use a colon (:) as shown below:

By using the colon (:), the interpreter will treat it as a list index range between 0 and 3 and will return the first 3 items of the list, as you can see below:

Another mistake the developers make while using the list in a python program is creating another list inside a list. See the example below:

student_names = [
                 ['Emile', 'Adam', 'Peter']

Let’s refer to an item on the list:

If you access an item from the above list, you will get the following error:

The problem with this piece of code is that a comma is missing between the items of the list. The solution is simple, just add the comma between the two lists to separate them. See the code below:

student_names = [
                 ['Emile', 'Adam', 'Peter']


Now, when you run the following command, you will get the second list as output:


In this article, we have learned about the list in python and how to solve the python error:  list indices must be integers or slices, not a tuple, error with the help of examples. We have also described the solutions to get rid of this problem.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content