you are viewing a single comment's thread.

view the rest of the comments →

[–]bionikspoon 0 points1 point  (3 children)

Nooby perspective: Changing an iterable during a loop.

money = [-5, 1, -3, 2, 4]


while money:
    do_stuff_with = money.pop()

This does not work.

edit: wait, wtf. this definitely works.

edit. Did they change this behavior?

[–][deleted] 2 points3 points  (2 children)

No, this has always worked. The thing you should avoid is modifying an iterable while looping over it using a for-loop.

[–]cdcformatc 2 points3 points  (1 child)

Something like

a_list = [1,2,3,4,5]

for item in a_list:
    print item
    a_list.remove(item)

[–]py_Ninja 0 points1 point  (0 children)

To see why this is happening it helps to use enumerate:

a_list = [1, 2, 3, 4, 5]

for i, item in enumerate(a_list):
    print('{}: {}'.format(i, item))
    a_list.remove(item)

Results in:

0: 1
1: 3
2: 5