You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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).
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.
The text was updated successfully, but these errors were encountered:
In the "Deleting a list item while iterating over it" trick, in the
list_1
example,del
actually deletes theitem
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 thedel item
statement. No operator even tries to delete from thelist_1
, and the list remains unaffected.There are 2 possible ways to fix this:
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 withlist_4.pop(idx)
.Just for historic reference, this is how the example looks now:
The text was updated successfully, but these errors were encountered: