What does RecursionError: maximum recursion depth exceeded mean in Python?
What is happening
Python limits how deep function calls can nest, and the default limit is commonly around 1000 frames. When a recursive function keeps calling itself and never reaches its stopping condition, the call stack fills up and Python raises RecursionError to protect the program.
The traceback will show the same function repeating many times. That repetition is the clue. Look at the function's stopping condition and check whether each call moves closer to it.
- Look for a repeated function name in the traceback
- Check that the input changes on each call
- Confirm the base case can actually be reached
How to fix it
Make sure the base case runs for every possible input, including empty values and zero. Then check that each recursive call passes a smaller or different input. A common bug is decreasing the wrong variable or returning the same argument.
If the recursion depth is legitimately large, rewrite the function with a loop and an explicit stack. Raising the recursion limit with sys.setrecursionlimit can hide the problem and may crash the interpreter, so use it only with care.
Tail recursion is not optimized in standard CPython, so rewriting the logic as a loop is often the cleanest fix. Test with a small input first to confirm the stopping condition works.
Common mistakes
- Raising the recursion limit without finding the missing base case.
- Forgetting that an empty list or zero input can skip the intended stopping point.

Related questions
- What does NullPointerException mean in Java?
- How do I fix IndexError: list index out of range in Python?
- What does TypeError: 'NoneType' object is not subscriptable mean?
- Why do I get a segmentation fault in C?
- What does ValueError: could not convert string to float mean?
- How do I fix a stack overflow error?