Sep 15, 2009

Python loop and modify list

When working with lists, don't modify them as you iterate through.
For example, if you need to remove empty strings in your list, don't delete the strings as you find them, as your loop is still iterating, and some items might be looped through twice, or not at all.

Solution:
Write out another list, or another variable to keep track of what your loop is trying to achieve.

Extra Tags:
Remove whitespace empty string list iterate modify for loop

No comments:

Post a Comment

So, what did you think?