Quick answer: Pygame rect.collidedict returning stale results after the dict is mutated mid-iteration? Python dict mutation invalidates iteration order - copy keys before iterating.
Inventory check iterates a dict, removes empty stacks. Some removed entries persist; others not.
Iterate over a copy
for key in list(d.keys()):
if condition: del d[key]List materializes keys; mutations don't affect the iteration.
Or build a delete list
Iterate once, collect deletes, apply after. Two passes; one consistent state.
Avoid collidedict with mutating callbacks
collidedict's callback should be pure. Side effects belong outside the call.
“Mutate-while-iterate is a bug in Python. Pygame inherits the bug class.”
Build a 'cleanup' helper that takes a dict and a predicate. Centralizes the pattern; the bug class stops recurring.