Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error in FOR
#3
(Jun-21-2018, 06:11 PM)Antigr Wrote: del b[i]
If you delete the item at i, then everything past that index moves over to the new index. So with the next iteration of the loop, i' refers to the item after what you probably expected it to be.

>>> spam = list(range(5))
>>> for ndx in range(len(spam)):
...   print(f"{ndx} => {spam[ndx]}")
...   del spam[ndx]
...
0 => 0
1 => 2
2 => 4
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
IndexError: list index out of range
That's why you're really not supposed to add/remove items from a list while also iterating over that same list. You could instead build a new list with the things you want to keep, or use a while loop instead of a for loop, for example.

>>> spam = [0 for _ in range(5)]
>>> spam.append(1)
>>> spam
[0, 0, 0, 0, 0, 1]
>>> foo = []
>>> for item in spam:
...   if item != 0:
...     foo.append(item)
...
>>> foo
[1]
>>> # or, if you prefer a comprehension...
...
>>> foo = [item for item in spam if item != 0]
>>> foo
[1]
>>> # or, if you prefer functional approaches...
...
>>> foo = list(filter(lambda item: item!=0, spam))
>>> foo
[1]
Reply


Messages In This Thread
error in FOR - by Antigr - Jun-21-2018, 06:11 PM
RE: error in FOR - by j.crater - Jun-21-2018, 06:22 PM
RE: error in FOR - by nilamo - Jun-21-2018, 06:28 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020