How to solve typeerror: list indices must be integers or slices, not tuple

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…

The post How to solve typeerror: list indices must be integers or slices, not tuple appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored 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']]

The post How to solve typeerror: list indices must be integers or slices, not tuple appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Deven


Print Share Comment Cite Upload Translate Updates
APA

Deven | Sciencx (2021-03-15T11:03:00+00:00) How to solve typeerror: list indices must be integers or slices, not tuple. Retrieved from https://www.scien.cx/2021/03/15/how-to-solve-typeerror-list-indices-must-be-integers-or-slices-not-tuple/

MLA
" » How to solve typeerror: list indices must be integers or slices, not tuple." Deven | Sciencx - Monday March 15, 2021, https://www.scien.cx/2021/03/15/how-to-solve-typeerror-list-indices-must-be-integers-or-slices-not-tuple/
HARVARD
Deven | Sciencx Monday March 15, 2021 » How to solve typeerror: list indices must be integers or slices, not tuple., viewed ,<https://www.scien.cx/2021/03/15/how-to-solve-typeerror-list-indices-must-be-integers-or-slices-not-tuple/>
VANCOUVER
Deven | Sciencx - » How to solve typeerror: list indices must be integers or slices, not tuple. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/15/how-to-solve-typeerror-list-indices-must-be-integers-or-slices-not-tuple/
CHICAGO
" » How to solve typeerror: list indices must be integers or slices, not tuple." Deven | Sciencx - Accessed . https://www.scien.cx/2021/03/15/how-to-solve-typeerror-list-indices-must-be-integers-or-slices-not-tuple/
IEEE
" » How to solve typeerror: list indices must be integers or slices, not tuple." Deven | Sciencx [Online]. Available: https://www.scien.cx/2021/03/15/how-to-solve-typeerror-list-indices-must-be-integers-or-slices-not-tuple/. [Accessed: ]
rf:citation
» How to solve typeerror: list indices must be integers or slices, not tuple | Deven | Sciencx | https://www.scien.cx/2021/03/15/how-to-solve-typeerror-list-indices-must-be-integers-or-slices-not-tuple/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.