• Register
+2 votes


I am trying to learn python3 and tuple. It was going pretty smooth unless I tried to access an element from a tuple of my program. It throws back an error: typeerror: tuple indices must be integers or slices, not tuple

What's wrong? Any explanation?

ago by (6k points)  

1 Answer

+1 vote


In python list and tuple are just simple sequences. We can only access their items by counting. Anyway, items stored in a dictionary are accessed through keys and not by counting as their items have no particular order. You can use tuples as keys of a dict, but an index or a slice makes no sense for a dict, just as a key makes no sense for a list or a tuple.

>>> t = (1, 2, 3)
>>> i = 0
>>> t[i]

See the above sample code to know the way of accessing a tuple element. As I can understand you were trying to access an element from a tuple by using another tuple. See the program below to understand how to do it:

>>> t = (1, 2, 3)
>>> k = 0
>>> i = k,
>>> print(i)

I hope I was able to make it understandable. Thanks.

ago by (17.9k points)