diff options
-rw-r--r-- | bot/resources/tags/loop-remove.md | 4 |
1 files changed, 0 insertions, 4 deletions
diff --git a/bot/resources/tags/loop-remove.md b/bot/resources/tags/loop-remove.md index 0fb22a06b..8245c7d93 100644 --- a/bot/resources/tags/loop-remove.md +++ b/bot/resources/tags/loop-remove.md @@ -3,7 +3,6 @@ embed: title: "Removing items inside a for loop" --- 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] @@ -13,9 +12,7 @@ for item in data: 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 # Start the loop: [1, 2, 3, 4] # First iteration: point to the first element @@ -28,7 +25,6 @@ print(data) ^ # Done ``` - You can avoid this pitfall by: - using a list comprehension to produce a new list (as a way of filtering items): ```py |