0

So, i have 2 loops, one inside of another, both having lists to iterate and work with, but both break when the first finishes iteration (so, outer one breaks prematurely). I really want to hear why this happens. And also, what to do to perform stuff in the inner loop for the whole list that outer loop uses? Output is at the end

kk = list(range(1, 10))  # [1,2,3,4,5,6,7,8,9]
    for l in kk:  # for example, l = 2
            for d in [2, 3]:  # d = 2
                g = l * d   # l = 2, g = 4
                if g >= 9:
                    break
                else:
                    kk.remove(g) # should remove 4 from kk, but did not
                    print(kk)
                    continue

            continue



    C:\Users\Denis\Python\Python36-32\python.exe C:/Users/Denis/PycharmProjects/interesting_stuff/other
[1, 3, 4, 5, 6, 7, 8, 9]
[1, 4, 5, 6, 7, 8, 9]
[1, 4, 5, 6, 7, 9]

Process finished with exit code 0
  • [\[SO\]: Remove items from a list while iterating](https://stackoverflow.com/questions/1207406/remove-items-from-a-list-while-iterating). – CristiFati Jan 27 '18 at 20:55

1 Answers1

0

you started with d = 2, l = 1. this removed 2 out of your list.

next time loop starts, l will be 4, and not 2.

Ido Kadosh
  • 81
  • 8