RuntimeError in Python — What It Means and How to Fix It

What Python's RuntimeError exception means and how to resolve it.

Published October 28, 2026

RuntimeError is a generic error for problems that do not fit other categories, such as modifying a dictionary while iterating or running an event loop twice.

What it means and why it happens

Message text is the key: it tells you what actually went wrong.

Read the full message: it names the file and line where the problem was detected, which is the best place to start.

What you see

run the code
RuntimeError: dictionary changed size during iteration

Practical examples

Code that triggers it

d = {'a': 1, 'b': 2}
for k in d:
    del d[k]

The fix

d = {'a': 1, 'b': 2}
for k in list(d):
    del d[k]

Comprehension instead

d = {k: v for k, v in d.items() if v > 1}

Common mistakes

  • Mutating a collection while looping.
  • Calling asyncio.run inside a running loop.
  • Ignoring the exact message.

Key takeaways

  • RuntimeError: RuntimeError is a generic error for problems that do not fit other categories, such as modifying a dictionary while iterating or running an event loop twice.
  • Read the message and the line number first.
  • Fix the cause, don't just silence the error.

FAQ

Common RuntimeError messages?

'dictionary changed size during iteration' and 'event loop is already running'.

How do I find which line caused RuntimeError?

The error message and stack trace show the file and line; start there and work backwards.

More Python articles