From 8b56a83968f40278e4ba3b42daf3027ec92bfb78 Mon Sep 17 00:00:00 2001 From: Galen Rice Date: Tue, 16 Apr 2024 12:26:15 -0400 Subject: fix: condense some whitespace around code blocks --- bot/resources/tags/loop-remove.md | 4 ---- 1 file changed, 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 -- cgit v1.2.3