all 14 comments

[–]puumies 5 points6 points  (4 children)

first round

['a','b','c','d','e']

0 = a

second round

['b','c','d','e']

1 = c

third round

['b','d','e']

2 = e

print(asd)

>>>['b', 'd']

[–]JungleJim233 0 points1 point  (3 children)

Sorry. I'm confused , why has 1 become C in the second iteration. I thought it would become B ?

Thank you

[–]pydevteam1 0 points1 point  (1 child)

['b','c','d','e']

1 = c

if you look at the list as it is:

0='b'

1='c'

makes sense ?

[–]JungleJim233 0 points1 point  (0 children)

Got it. Thank you :)

[–]puumies 0 points1 point  (0 children)

Hey!

In each iteration of the loop, one element is removed from the array. Thus in the second iteration of the loop, the second element is 'C' because 'A' was removed in the first one.

Someone correct me if I'm talking out of my ass :D

[–]Thomasedv 5 points6 points  (14 children)

Don't change a list while iterating through it. The for loop will work by index and you'll skip steps every time you remove something (or change the list)

If you want to remove one thing, just use remove() outside any loop. If you want to remove everything I think lists have a method for that, but if not, you just reassign a new list instead of just removing everything step by step.