• Register
3 votes
174 views

Problem:

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?

12 7 7
15,250 points

2 Answers

1 vote

Solution:

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]
1

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)
(0,)

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

13 9 6
94,240 points
0 votes

Solution:

Tuple end list indices must be ints or slices, because tuples and lists are just simple sequences. You can only access their items by counting. However, items stored in a dictionary are accessed through keys and not by counting as their items have no particular order.

When you try to access a member of a list or tuple, which I suppose image is you do it with an integer inside sqare brackets like so :

image[0]

I think you are maybe trying to do this:

image[s][t]

This would access the int(s) member of image list/tuple. If this member happens to be a list also, you can access it's memebers by adding another square bracket with an inte inside it specifying the index of this member. If your loop is taking you through matrix of pixels in an image this makes sense also since with the first loop you might be going through rows of pixels and with the second through columns and try to retreive the RGB values.

10 6 4
31,120 points

Related questions

0 votes
1 answer 10 views
1 vote
2 answers 24 views
24 views
Problem: I wrote a program: def only(): names = ['Laia', 'Poter', 'Harrold', 'Annabell', 'Preety', 'Whina', 'Cory', 'Phase'] put = input('Search with name:') binary_search(names, put) if position == -1: print("The name entered is not part ... in binary_search if names[middle] == entered: TypeError: list indices must be integers or slices, not float Why is this error happenning can anyone explain?
asked Apr 3 LizzyM 6.1k points
1 vote
1 answer 21 views
21 views
Problem: I have a program where I used tuple and it is throwing e some eror message. The code I worte is : import random board = [] for x in range(0,5): board.append(["O"] * 5) def print_board(board): for row in board: print " ".join(row) print_board(board) ... len(board)-1 or guess_col < 0 or guess_col > len(board[0])-1: TypeError: list indices must be integers not tuple How to solve this thing?
asked Apr 27 samhaz 5k points
1 vote
3 answers 551 views
551 views
Problem : I'm a newbie with Python's arrays and I don't understand how I can avoid below error: python typeerror: list indices must be integers or slices, not str
asked Oct 21, 2019 peterlaw 6.9k points
1 vote
1 answer 54 views
54 views
Problem: I wrote a program in which gives me the following error : TypeError: startswith first arg must be bytes or a tuple of bytes, not str The program I wrote is: def allFasta(filename): xd = open(filename, 'yb') headers = "" sequence = "" while True: ... ("youn.xd") Why is this type error message is appearing for this program. Is there any problem with my programs arguments? Thanks in advance.
asked Jun 11 samhaz 5k points