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
A nice addition to your "not so obvious" notebook would be the modifying a list while looping through it pitfall. For instance, you might be tempted into believing that the following will remove all even values from the list a
I'll leave it as an exercise for you to figure out exactly what is going on here.
Dictionaries will protect you from this by raising an exception if they change during iteration, but for lists, you should use a copy (for i in a[:]), or convert the for loop into a while loop.
Great notebook by the way!
The text was updated successfully, but these errors were encountered:
A nice addition to your "not so obvious" notebook would be the modifying a list while looping through it pitfall. For instance, you might be tempted into believing that the following will remove all even values from the list
a
But if you try a different example:
I'll leave it as an exercise for you to figure out exactly what is going on here.
Dictionaries will protect you from this by raising an exception if they change during iteration, but for lists, you should use a copy (
for i in a[:]
), or convert thefor
loop into awhile
loop.Great notebook by the way!
The text was updated successfully, but these errors were encountered: