diff options
author | 2024-04-16 12:26:15 -0400 | |
---|---|---|
committer | 2024-04-16 12:26:15 -0400 | |
commit | 8b56a83968f40278e4ba3b42daf3027ec92bfb78 (patch) | |
tree | bbb422f9b0770ab246f179edd9e7ff7e629ab043 | |
parent | feat: tag loop-remove (diff) |
fix: condense some whitespace around code blocks
-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 |