• Register
3 votes
284 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,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 7 views
7 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 9.6k points
0 votes
1 answer 50 views
50 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 6k points
0 votes
1 answer 30 views
1 vote
2 answers 89 views
89 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 37 views
37 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 4 views
4 views
typeerror: tuple indices must be integers or slices, not tuple
asked Feb 16 ArifulIslam 7.5k points
0 votes
1 answer 15 views
15 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 9.6k points
0 votes
1 answer 8 views
8 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 9.6k points
1 vote
3 answers 655 views
655 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
0 votes
1 answer 6 views
6 views
Problem: I've got two lists of coordinates, they look like this: list_kp2_ok: [[1185.60009765625, 933.6000366210938], [1310.4000244140625, 828.0000610351562], [1067.0, 979.0], [1310.0, 828.0], [1423.2000732421875, 814.800048828125], [1306.0, 828.0], [3634.0, 605.0 ... 828.6107788085938]] Now I need to check if there are any same coordinates on both lists and create a new list of them. So I wrote:
asked Feb 17 Mashhoodch 9.6k points