Python: TypeError: 'int' object is not subscriptable
list, python, tuples, typeerror
Solution
You are indexing into each individual tuple:
c = t[i][0]
`i` starts out as `0`, but you increment it each loop iteration:
i += 1
The `for` loop is binding `t` to each individual tuple from `l_text`, so first `t` is bound to `('a', 0)`, then to `('b', 1)`, etc.
So first you are looking at `('a', 0)[0][0]` which is `'a'[0]` which is `'a'`. The next iteration you look at `('b', 1)[1][0]` which is `1[0]` which raises your exception, because integers are not sequences.
You need to remove the `i`; you do not need to keep a running index here as the `for t in l_text:` is already giving you each individual tuple.
Problem
I get a TypeError and I don't understand why. The error is at `c = t[i][0]` (according to the debugger). I have 3 char groups(lists): `g1`, `g2` and `g3` and I'm trying to change the char's index by substracting the key's `k1`, `k2` or `k3` from the index. What I'm using now for testing: ``` text = 'abcd' l_text = [('a', 0), ('b', 1), ('c', 2), ('d', 3)] k1, k2, k3 = 2, 3, 1 ``` And this is the code: ``` def rotate_left(text, l_text, k1, k2, k3): i = 0 newstr = [None]*len(text) for t in l_text: # t = tuple c = t[i][0] if c in g1: # c = char l = int(l_text[i][1]) # l = index of the char in the list if l - k1 < 0: newstr[l%len(text)-k1] = l_text[i][0] else: newstr[l-k1] = l_text[i][0] elif c in g2: l = l_text[i][1] # l = index of the char in the list if l - k1 < 0: newstr[l%len(text)-k2] = l_text[i][0] else: newstr[l-k2] = l_text[i][0] else: l = l_text[i][1] # l = index of the char in the list if l - k1 < 0: newstr[l%len(text)-k3] = l_text[i][0] else: newstr[l-k3] = l_text[i][0] i += 1 return newstr ``` Can someone explain me why do I get this error and how do I fix it? It's not like I'm using an `int` type there. The debugger shows it's a str type and it breaks after the 2nd iteration. PS google didn't help PPS I know there is too much repetition in the code. I did it to see in the debugger what's happening. UPDATE: ``` Traceback (most recent call last): File "/hometriplerotatie.py", line 56, in <module> print(codeer('abcd', 2, 3, 1)) File "/home/triplerotatie.py", line 47, in codeer text = rotate_left(text, l_text, k1, k2, k3) File "/home/triplerotatie.py", line 9, in rotate_left c = t[i][0] TypeError: 'int' object is not subscriptable ```