Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Deleting a list item while iterating over it" has improper example or explanation #16

Closed
nolar opened this issue Sep 1, 2017 · 2 comments
Assignees

Comments

@nolar
Copy link

nolar commented Sep 1, 2017

In the "Deleting a list item while iterating over it" trick, in the list_1 example, del actually deletes the item variable name. The name will be reassigned to the next value on the next iteration, but will be absent up to the end of the code block after the del item statement. No operator even tries to delete from the list_1, and the list remains unaffected.

There are 2 possible ways to fix this:

  1. Either you meant del list_1[idx] in the example, which removes the indexed item from the list. And the output will be [2, 4] for the same reason as with list_4.pop(idx).
  2. Or the explanation is wrong, and should not refer to item deletion, but should refer to the variable name un-assignment.

Just for historic reference, this is how the example looks now:

list_1 = [1, 2, 3, 4]
for idx, item in enumerate(list_1):
    del item
>>> list_1
[1, 2, 3, 4]
* del removes a specific index (That's why first list_1 was unaffected), raises IndexError if an invalid index is specified.
@satwikkansal
Copy link
Owner

Ah, right, my only intention to add del was to show the abuse of the del keyword. I'll fix the explanation.

@satwikkansal satwikkansal self-assigned this Sep 4, 2017
@satwikkansal
Copy link
Owner

@nolar Thanks for reporting the issue! I've fixed this in 7e93e0c, feel free to reopen the issue if the explanation seems insufficient or incorrect 👍

BenSt099 added a commit to BenSt099/wtfpython that referenced this issue Aug 11, 2023
tothetop430 added a commit to tothetop430/wtfpython that referenced this issue Dec 9, 2023
* Corrects the explanation for `del` keyword.

Fixes satwikkansal/wtfpython#16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants