Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove() during iteration causes items to be skipped #44166

Closed
krabsa mannequin opened this issue Oct 24, 2006 · 2 comments
Closed

remove() during iteration causes items to be skipped #44166

krabsa mannequin opened this issue Oct 24, 2006 · 2 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@krabsa
Copy link
Mannequin

krabsa mannequin commented Oct 24, 2006

BPO 1584028
Nosy @rhettinger

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 = None
closed_at = <Date 2006-10-25.01:06:12.000>
created_at = <Date 2006-10-24.21:44:32.000>
labels = ['interpreter-core', 'invalid']
title = 'remove() during iteration causes items to be skipped'
updated_at = <Date 2006-10-25.01:06:12.000>
user = 'https://bugs.python.org/krabsa'

bugs.python.org fields:

activity = <Date 2006-10-25.01:06:12.000>
actor = 'rhettinger'
assignee = 'none'
closed = True
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2006-10-24.21:44:32.000>
creator = 'krabsa'
dependencies = []
files = []
hgrepos = []
issue_num = 1584028
keywords = []
message_count = 2.0
messages = ['30393', '30394']
nosy_count = 2.0
nosy_names = ['rhettinger', 'krabsa']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1584028'
versions = ['Python 2.4']

@krabsa
Copy link
Mannequin Author

krabsa mannequin commented Oct 24, 2006

If, when iterating over the contents of a list, the
current item is removed, the next item is skipped.

#Code:
if __name__ == '__main__':
  items = [0,1,2,3,4,5]
  for i in items:
    print i
    items.remove(i)

#End Code

Outputs:
0
2
4

I believe the behavior is undesirable. An argument can
be made to not fix, but the issue is worth noting.

@krabsa krabsa mannequin closed this as completed Oct 24, 2006
@krabsa krabsa mannequin added invalid interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Oct 24, 2006
@krabsa krabsa mannequin closed this as completed Oct 24, 2006
@krabsa krabsa mannequin added invalid interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Oct 24, 2006
@rhettinger
Copy link
Contributor

Logged In: YES
user_id=80475

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)]

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

1 participant