How to solve typeerror: list indices must be integers or slices, not tuple
Posted on: March 15, 2021 by Deven
In this article, you will learn how to solve typeerror: list indices must be integers or slices, not tuple error in Python.
Let’s look at a code example that produces the same error.
fruits = [
["mango", "banana", "Grapes"]
["orange", "guava", "pineapple"]
]
name = input("input the name of the fruit: ")
taste = input("input the taste of fruit: ")
fruit_color = input("imput the color of fruit ")
fruits.append([name, taste, fruit_color])
print(fruits)
Output
Traceback (most recent call last):
File "<string>", line 2, in <module>
TypeError: list indices must be integers or slices, not tuple
In order to solve typeerror: list indices must be integers or slices, not tuple error you need to include commas between the values in your list. consider the code example below:
fruits = [
["mango", "banana", "Grapes"], #we forgot to add comma above
["orange", "guava", "pineapple"]
]
name = input("input the name of the fruit: ")
taste = input("input the taste of fruit: ")
fruit_color = input("input the color of fruit ")
fruits.append([name, taste, fruit_color])
print(fruits)
output:
input the name of the fruit: mango
input the taste of fruit: sweet
input the color of fruit yellow
[['mango', 'banana', 'Grapes'], ['orange', 'guava', 'pineapple'], ['mango', 'sweet', 'yellow']]
Share on social media
//