# Iterating list in "for" loop, with deletions, skips members

**URL:** <https://discuss.python.org/t/iterating-list-in-for-loop-with-deletions-skips-members/12963>\
**Category:** Python Help\
**Created:** [January 8, 2022, 12:47am UTC](https://discuss.python.org/t/iterating-list-in-for-loop-with-deletions-skips-members/12963 "2022-01-08T00:47:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![emfocuser](https://avatars.discourse-cdn.com/v4/letter/e/b5e925/32.png) [@emfocuser](https://discuss.python.org/u/emfocuser)\
**Post date:** [January 8, 2022, 12:47am UTC](https://discuss.python.org/t/iterating-list-in-for-loop-with-deletions-skips-members/12963/1 "2022-01-08T00:47:33Z")

</div>

I create the list and print one member at a time and delete each.  
Why is it skipping all the odd numbers?  
_ **code** _  
L = [1,2,3,4,5,6,7,8]  
for x in L:  
print(x)  
L.remove(x)  
print(L)  
_output_  
1  
[2, 3, 4, 5, 6, 7, 8]  
3  
[2, 4, 5, 6, 7, 8]  
5  
[2, 4, 6, 7, 8]  
7  
[2, 4, 6, 8]

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [January 8, 2022, 1:30am UTC](https://discuss.python.org/t/iterating-list-in-for-loop-with-deletions-skips-members/12963/2 "2022-01-08T01:30:05Z")

</div>

It is skipping the even (not odd) numbers because you are deleting  
items, which moves the remaining items back one position.

Let’s take a simpler example:

```
L = [1, 2, 3]

```

When you iterate over L, there is an internal counter that points to the  
current _position_ in the list. That position starts at 0:

```
for x in L: ...

```

which gives x = 1. So you have:

Step 1:

- position 0
- L = [1, 2, 3]
- x = 1

Step 2: remove x from L

- position is still 0
- L = [2, 3]

Step 3: advance to the next position

- position → 1
- L = [2, 3]
- so x = 3

and you skipped 2.

The lesson here is:

**Never modify a list while you are iterating over it.**

For experts only: you can safely modify the list if you walk through the  
list _backwards_, removing items only from the end you have already  
visited. But if you’re an expert, you will also know that doing that is  
much more complicated and slower, so experts know enough to know that  
they rarely want to do it that way.

Instead, you should iterate over a temporary copy:

```
for x in L[:]: # Use a list slice to make a copy.
    print(x)
    L.remove(x) # Modify the original, not the copy.
    print(L)
```

---

<div class="post-metadata">

**Author:** ![emfocuser](https://avatars.discourse-cdn.com/v4/letter/e/b5e925/32.png) [@emfocuser](https://discuss.python.org/u/emfocuser)\
**Post date:** [January 8, 2022, 10:41pm UTC](https://discuss.python.org/t/iterating-list-in-for-loop-with-deletions-skips-members/12963/3 "2022-01-08T22:41:58Z")

</div>

Thanks,  
I don’t see an option to “mark as answer”  
Pete

---

<div class="post-metadata">

**Author:** ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)\
**Post date:** [January 10, 2022, 1:21am UTC](https://discuss.python.org/t/iterating-list-in-for-loop-with-deletions-skips-members/12963/4 "2022-01-10T01:21:03Z")

</div>

> [@emfocuser](#):
>
> I don’t see an option to “mark as answer”

Because this is a discussion forum, not Stack Overflow 🙂

---

<div class="post-metadata">

**Author:** ![erlendaasland](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erlendaasland/32/4378_2.png) [@erlendaasland](https://discuss.python.org/u/erlendaasland)\
**Post date:** [January 10, 2022, 10:02am UTC](https://discuss.python.org/t/iterating-list-in-for-loop-with-deletions-skips-members/12963/5 "2022-01-10T10:02:33Z")

</div>

Actually, Discourse has an “Accepted” plugin, but it is not activated here.

> **[Discourse Solved (Accepted answer plugin)](https://meta.discourse.org/t/discourse-solved-accepted-answer-plugin/30155)**
>
> Summary: The Discourse Solved plugin allows users to accept solutions on topics in designated categories. 🔗 GitHub: GitHub - discourse/discourse-solved: Allow accepted answers on topics ➡ Install: Follow the plugin installation guide. ...

---

<div class="post-metadata">

**Author:** ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)\
**Post date:** [January 10, 2022, 10:41pm UTC](https://discuss.python.org/t/iterating-list-in-for-loop-with-deletions-skips-members/12963/6 "2022-01-10T22:41:53Z")

</div>

Interesting; can this be activated on a per top-level-category basis? If so, might be worth having a separate category for questions (which probably half or more of `Users`, by far the most active top-level-category on this Discourse at least in terms of posts.

---

<div class="post-metadata">

**Author:** ![erlendaasland](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/erlendaasland/32/4378_2.png) [@erlendaasland](https://discuss.python.org/u/erlendaasland)\
**Post date:** [January 11, 2022, 12:04am UTC](https://discuss.python.org/t/iterating-list-in-for-loop-with-deletions-skips-members/12963/7 "2022-01-11T00:04:14Z")

</div>

I have no idea, but something like that would make sense. Cc. [@admins](https://discuss.python.org/groups/admins)
