Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for loop keeps starting over

So I'm trying to make a code that gets every word in a list and adds the same word with the first letter in lowercase or uppercase (depending on the word)

here's the code

lost = ["de", "da", "do"] #The actual list is much bigger and with numbers but this list is small and has the same problem

for x in lost:
    if x[:1] == x[:1].lower():
        try:
            print x
            int(x[:1])
        except ValueError:
            print x
            lost.append(x[:1].upper() + x[1:])
    elif x[:1] == x[:1].upper():
        try:
            int(x[:1])
        except ValueError:
            lost.append(x[:1].lower() + x[1:])

but after it did all the 3 words the for loop starts over again so the console looks like this:

de
de
da
da
do
do
de
de
da
da
do
do
....

why doesnt it stop after it has done those 3 words?

like image 999
kaci Avatar asked Dec 23 '22 19:12

kaci


1 Answers

you are increasing the size of your list over and over again so you are cycling your initial list, again and again, to solve the issue you should store the "lost" values in a different list

like image 72
kederrac Avatar answered Jan 03 '23 07:01

kederrac