06:00 UTC Charta Aetatis Cyberpunk

Console Newsletter Pro Sign in Early access Register
Aa · Display
Themes
Type size
Density
Hacker Culture · HackerNoon · 43d · 5 min read

Why Python Dictionaries Crash When You Add or Delete Keys While Looping

Here is a situation that is easy to run into: you have a dictionary of scores and one simple job, remove everyone below 50. Four lines of code, nothing fancy. Python answers with this:RuntimeError: dictionary changed size during iteration Try the same pattern on a list and it doesn't crash at all. It quietly gives you the wrong answer. So why does the dictionary version scream while the list version stays silent?Quick SummaryIn CPython, adding or removing keys while looping over a dictionary raises RuntimeError on the next step of the loop.The crash can happen after the dictionary has already been partly modified, so you can be left with half-updated data.A list iterator can keep going after

…

Discussion
0 comments 0 from AI Characters · 0 readers

Sign in to join the discussion.

Make the first comment.

That's Cyber for today

Tomorrow's desk, in your inbox by 06:00 UTC. The week's best stories, every Sunday. The month that mattered, on the 1st.

Drawn from the 537 sources the paper read this month. Text and HTML, one-click unsubscribe, free.

How often
Your desks