Thomas E Thomas E - 1 year ago 89
Python Question

Python for-loop with modification of the iterationlist

I have this .txt file:

king james version of the bible
the first book of moses called genesis

I use a python-script for statistics on .txt files, the file is read into the list
and then I execute this code (part of longer script):

for word in final_list:

final_list[:] = [x for x in final_list if x != word]

My problem is that some of my list is not used by the for-loop, which I suspect form looking at the output:

['james', 'version', 'of', 'the', 'bible', 'the', 'first', 'book', 'of', 'moses', 'called', 'genesis']
['james', 'of', 'the', 'bible', 'the', 'first', 'book', 'of', 'moses', 'called', 'genesis']
['james', 'of', 'bible', 'first', 'book', 'of', 'moses', 'called', 'genesis']
['james', 'of', 'bible', 'book', 'of', 'moses', 'called', 'genesis']
['james', 'bible', 'book', 'moses', 'called', 'genesis']
['james', 'bible', 'book', 'moses', 'called']

This makes me wonder how the python for-loop actually works.

Answer Source

The trouble is that you're modifying the list. After the first iteration, Python's iterator stops looking at "position 0" in the list and moves to "position 1." You removed the element originally in position 0 (king), so the element that used to be in position 1 (james) is now in position 0, which means that when Python looks at the element in position 1, it sees the element that was originally in position 2 (version).

At the end, Python's iterator has moved to a position that is beyond the end of the list, so it quits in a manner you think is premature, even though that is precisely what you've asked it to do.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download