In <[EMAIL PROTECTED]> John Machin <[EMAIL PROTECTED]> writes:
>It's nothing to do with list comprehensions, which are syntactical
>sugar for traditional loops. You could rewrite your list comprehension
>in the traditional manner...
>and it would still fail for the same reason: mutating the list over
>which you are iterating.
I normally deal with this problem by iterating backwards over the
indices. Here's how I coded the function (in "Python-greenhorn
style"):
def cull(list):
culled = []
for i in range(len(list) - 1, -1, -1):
if not_wanted(list[i]):
culled.append(list.pop(i))
return culled
...where not_wanted() is defined elsewhere. (For my purposes at the
moment, the greater generality provided by making not_wanted() a
parameter to cull() was not necessary.)
The specification of the indices at the beginning of the for-loop
looks pretty ignorant, but aside from that I'm happy with it.
Kynn
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
--
http://mail.python.org/mailman/listinfo/python-list