Lists are indexed using numbers. This means if you want to access an item from a list, you have to refer to its index position. If you specify a tuple as a list index value, you encounter the “TypeError: list indices must be integers, not tuple” error.
In this guide, we talk about what this error means and where you may encounter it. We walk through an example of this error so you can learn how to overcome it in your code.
TypeError: list indices must be integers, not tuple
Lists are indexed starting from the value 0. Every subsequent value has an index number 1 greater than the last. Consider the following list:
coffee_growers = ["Ethiopia", "Kenya", "Rwanda", "Brazil"]
This list has four values. Ethiopia has an index value 0, Kenya has an index value 1, and so on. To access items from this list, we reference these values:
print(coffee_growers[0])
Our code returns “Ethiopia”.
We cannot specify a tuple value to access an item from a list. This is because tuples do not correspond to any index value in a list.
The “TypeError: list indices must be integers, not tuple” error is most common in a list of lists that is missing a comma separator between each value in the list.
An Example Scenario
We’re going to build a program that tracks information about some UK birds. Start by defining a list of birds:
birds = [ ["Barn Owl", "Owl", "Green"] ["Black tern", "Gulls and terns", "Green"] ]
The first item in each list is the name of a bird; the second item is the family of a bird; the third item is the UK conservation status of that bird. Next, we ask a user to add a record to our list. We do this using the input() method:
name = input("Enter the name of the bird: ") family = input("Enter the family of the bird: ") conservation_status = input("Enter the conservation status of the bird: ")
Now that we’ve collected this data, we can add information about a bird to our list of birds. We do this using the append()
method:
birds.append([name, family, conservation_status]) print(birds)
Our code prints out our new list of birds after the record we want to add is added to the list. Let’s run our code and see what happens:
Traceback (most recent call last): File "main.py", line 2, in <module> ["Barn Owl", "Owl", "Green"] TypeError: list indices must be integers or slices, not tuple
Our code returns an error. Notice our error occurs before we are asked to insert information about a bird. If we look at our stack trace, we see the problem is on line two of our code, which is within our list declaration.
The Solution
The problem with our code is that we’ve forgotten to include commas between the values in our list. This causes an issue because without commas our list interprets the second record as an index value for the first record:
birds = [ ["Barn Owl", "Owl", "Green"] ["Black tern", "Gulls and terns", "Green"] ]
Python reads this code as:
["Barn Owl", "Owl", "Green"]["Black tern", "Gulls and terns", "Green"]
The second list cannot be an index value for the first list. Index values must be numbers. Our code says that we’ve specified a tuple because our second list is reduced so it has multiple values.
To solve this problem, we must separate the lists in our list of lists using a comma:
birds = [ ["Barn Owl", "Owl", "Green"], ["Black tern", "Gulls and terns", "Green"] ]
Try to run our code with the comma in the birds list:
Enter the name of the bird: Pied Wagtail Enter the family of the bird: Pipits and wagtails Enter the conservation status of the bird: Green [['Barn Owl', 'Owl', 'Green'], ['Black tern', 'Gulls and terns', 'Green'], ['Pied Wagtail', 'Pipits and wagtails', 'Green']]
Our code executes successfully. First, we’re asked to insert information about a bird. Our code then adds that information to our list of birds. Finally, our code prints out the list of birds. We see our new record is added to the end of the list.
Conclusion
The “TypeError: list indices must be integers, not tuple” error occurs when you specify a tuple as an index value at the end of a list. To solve this problem, make sure that any lists in a list of lists are separated using commas.
Now you’re ready to solve this common Python error like a professional developer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.