$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · medium · #59

What is the correct fix?

counts = {"a": 1, "b": 0}
for k in counts:
    if counts[k] == 0:
        del counts[k]
  1. Iterate a copy: for k in list(counts)
  2. Iterate a copy: for k in dict(counts[k])
  3. Change del counts[k] to counts.pop()
  4. Change the test to if not counts[k]:
Deleting while iterating a dict raises RuntimeError; iterate over a snapshot of the keys.
Play Problem Solving ▸