I'm trying to insert an item from one list to another and using an item from a list of numbers for the index but I'm getting this error even though I'm using integers for index numbers
and the other thing is that the same item is accepted as an index in the line just before the error
I even tried putting a number there just to test it but it gave me the same error
here's the code:
FoundLetters = ['p', 'l', '-', 'i', 'n']
MissingLetters = []
AllLetters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
def MissingCount():
for j in range(len(FoundLetters)):
if FoundLetters[j] == '-':
MissingLetters.append(int(j))
def iterate():
for i in range(len(AllLetters)):
for i in AllLetters:
FoundLetters.pop(MissingLetters[0])
FoundLetters.insert(MissingLetters[0], AllLetters[i])
MissingCount()
iterate()
and this was the exact error:
Traceback (most recent call last):
File "main.py", line 26, in <module>
iterate()
File "main.py", line 22, in iterate
FoundLetters.insert(MissingLetters[0], AllLetters[i])
TypeError: list indices must be integers or slices, not str
** Process exited - Return Code: 1 **
Press Enter to exit terminal
the problem with your code is that you are using i twice in the iterate function. So you are overwriting the first i. Therefore you are looping through AlLLetters and you try to index AllLeters with strings like "a". Try to change the second loop