diff options
-rw-r--r-- | bot/resources/tags/loop-remove.md | 5 |
1 files changed, 1 insertions, 4 deletions
diff --git a/bot/resources/tags/loop-remove.md b/bot/resources/tags/loop-remove.md index 8245c7d93..c46cb47ba 100644 --- a/bot/resources/tags/loop-remove.md +++ b/bot/resources/tags/loop-remove.md @@ -4,13 +4,10 @@ embed: --- Avoid removing items from a collection, such as a list, as you iterate that collection in a `for` loop: ```py -# Don't do this! data = [1, 2, 3, 4] for item in data: data.remove(item) - -print(data) -# [2, 4] # <- every OTHER item was removed! +print(data) # [2, 4] <-- every OTHER item was removed! ``` `for` loops track the index of the current item with a kind of pointer. Removing an element causes all other elements to shift, but the pointer is not changed: ```py |