• Register
3 votes
472 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

Please log in or register to answer this question.

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,260 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 28 views
28 views
Problem: So, I'm not entirely sure what's going on here, but for whatever reason Python is throwing this at me. For reference, it's part of a small neural network I'm building for fun, but it uses a lot of np.array and such, so there's a lot of matrices ... type clash. Maybe somebody can help me figure this out, because I've been staring at this error for too long without being able to fix it.
asked Feb 16 Mashhoodch 13k points
0 votes
1 answer 153 views
153 views
Problem: Hello! I am completely new to Python and trying to grasp concepts in Python. While I was learning lists and dictionaries, I wrote the following program: name = ["John", "Alice", "Catherine"] diction = {"name" : "Alice"} print(name[diction]) When I ... ; as a value and the program should print “Alice” on screen. I do not know what I have done wrong. Can anybody help?
asked Dec 18, 2020 Code Learner 9.8k points
0 votes
1 answer 45 views
1 vote
2 answers 196 views
196 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, 2020 LizzyM 6.1k points
1 vote
1 answer 43 views
43 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, 2020 samhaz 5k points
0 votes
1 answer 12 views
12 views
typeerror: tuple indices must be integers or slices, not tuple
asked Feb 16 ArifulIslam 7.5k points
0 votes
1 answer 49 views
49 views
Problem: I am trying to pull data from a database and assign them to different lists. This specific error is giving me a lot of trouble "TypeError: tuple indices must be integers, not str" I tried converting it to float and etc but with no success. Any help would be appreciated! Thanks people!
asked Mar 16 ummesalma 29.2k points
0 votes
1 answer 30 views
30 views
Problem: I have been banging my head against this for two days now. I am new to python and programming so the other examples of this type of error have not helped me to much. I am reading through the documentation for lists and tuples, but haven't found anything ... be much appreciated. Not looking for the answer necessarily, just more resources on where to look. I am using Python 2.7.6. Thanks
asked Jan 27 Mashhoodch 13k points
0 votes
1 answer 23 views
23 views
Problem: I have been banging my head against this for two days now. I am new to python and programming so the other examples of this type of error have not helped me to much. I am reading through the documentation for lists and tuples, but haven't found anything ... would be much appreciated. Not looking for the answer necessarily, just more resources on where to look. I am using Python 2.7.6.
asked Jan 26 Mashhoodch 13k points
0 votes
1 answer 2 views
2 views
Problem: When running the code in IDLE gives the following error: Typeerror: slice indices must be integers or none or have an __index__ method. How to solve this?
asked Apr 21 muktaa 34.6k points