Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: list indices must be integers

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading