You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2006-10-25.01:06:12.000>created_at=<Date2006-10-24.21:44:32.000>labels= ['interpreter-core', 'invalid']
title='remove() during iteration causes items to be skipped'updated_at=<Date2006-10-25.01:06:12.000>user='https://bugs.python.org/krabsa'
Sorry, this isn't a bug -- it is a natural side-effect of
mutating a object while iterating over it. The various
approaches to dealing with this include:
don't allow mutation while iterating -- dict.iterkeys()
uses this approach
iterate over a copy of the object -- dict.keys() uses this
approach
iterate over consecutive indices and ignore mutation --
lists use this approach
Programmers can avoid dealing with this issue by:
precopying the list:
for i in items[:]:
print i
remove(i)
building a new list during iteration:
items[:] = [x for x in items if f(x)]
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: