CS concepts
Recursion vs Iteration Explained: When to Use Each in Python
Updated Nov 27, 2026 · 3 min read
Use a loop for long linear work and anything that might go 1,000 or more levels deep: every recursive call adds a stack frame, CPython's default recursion limit is 1,000, and Python has no tail-call optimisation. Use recursion for trees, graphs, backtracking and divide and conquer when the depth stays well under 1,000, because the call stack remembers your path for you.

Key points
- ●Every recursive call pushes a stack frame; past CPython's default limit of 1,000 you get RecursionError.
- ●Linear recursion uses O(n) stack space; a loop uses O(1).
- ●Python doesn't do tail-call optimisation, so tail recursion still grows the stack.
- ●Recursion wins for trees, graphs and backtracking: max depth of a binary tree is one line.
- ●Naive fib(30) makes 2,692,537 calls. Cache it or loop it and it's 30 steps.

Two ways to repeat work
Recursion is a function that calls itself on a smaller input until it hits a base case. Iteration is a for or while loop that updates a few variables. Anything you can write one way, you can write the other way.
In Python the choice matters more than in many languages, because of one number: 1,000. The visual sheet above scores the two across seven points. They come down to four rounds.
Round 1: depth
Every recursive call pushes a new stack frame. CPython's default recursion limit is 1,000 (check it with sys.getrecursionlimit()). Go past it and you get:
RecursionError: maximum recursion depth exceeded
A loop doesn't stack anything. It can run a million times with no frames piling up.
Loop wins.
Round 2: memory
Linear recursion, one call per item, holds n frames at once, so it uses O(n) extra space. A loop keeps a couple of variables, O(1).
You might hope tail recursion fixes this, since some languages reuse the frame when the recursive call is the last thing a function does. CPython doesn't. Guido van Rossum rejected tail-call optimisation in his 2009 post "Tail Recursion Elimination", partly because it would drop frames from tracebacks. A tail-recursive Python function still grows the stack.
Loop wins again.
Round 3: trees, graphs and backtracking
This is where recursion earns its place. In DFS, permutations, subsets, merge sort and quicksort, you need to remember where you were so you can come back. The call stack does that for you.
Max depth of a binary tree is one line of real logic:
def depth(node):
if not node:
return 0
return 1 + max(depth(node.left), depth(node.right))
The loop version needs your own stack, a Python list, and code to push and pop by hand. Recursion keeps these solutions to 3 to 5 lines, and it matches how divide-and-conquer problems are defined, like fib(n) = fib(n-1) + fib(n-2).
Recursion wins.
Round 4: the Fibonacci trap
Matching the definition can go badly wrong. Naive recursive fib(30) makes 2,692,537 calls, because it recomputes the same smaller values again and again.
Two fixes, both 30 steps:
from functools import cache
@cache
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
def fib_loop(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
@cache keeps the recursive shape and remembers each answer. The loop just walks up from 0 and 1. Both give fib(30) == 832040.
The scorecard
| Recursion | Iteration | |
|---|---|---|
| Depth | Default limit 1,000, then RecursionError | Runs a million times, no frames |
| Extra memory, linear work | O(n) stack | O(1) |
| Tail calls | Not optimised in CPython | Not needed |
| Trees, graphs, backtracking | 3 to 5 lines, stack is automatic | Needs an explicit stack |
| Naive fib(30) | 2,692,537 calls (30 with @cache) | 30 steps |
What to pick
- Recursion for trees, graphs, backtracking and divide and conquer, when the depth stays well under 1,000.
- A loop for long linear work, big inputs, and anything that might go 1,000 or more levels deep.
The trap to watch for
Reaching for sys.setrecursionlimit when you hit the error. It raises the limit, but going very deep can still crash the interpreter. If the input can be that deep, the real fix is a loop with your own stack. Any recursion can be rewritten that way, and for linear work the loop is usually shorter anyway.
Frequently asked questions
What is Python's recursion limit?
CPython's default recursion limit is 1,000, which you can check with sys.getrecursionlimit(). Go past it and Python raises RecursionError: maximum recursion depth exceeded.
Should I raise the limit with sys.setrecursionlimit?
You can, but going very deep can still crash the interpreter. If your recursion might go past 1,000 levels, rewrite it as a loop, using a Python list as your own stack if you need one.
Does Python optimise tail recursion?
No. CPython has no tail-call optimisation. Guido van Rossum rejected it in his 2009 post "Tail Recursion Elimination", partly because it would drop frames from tracebacks. A tail-recursive function still grows the stack.
Is recursion slower than iteration in Python?
It can be much slower when calls repeat work. Naive recursive fib(30) makes 2,692,537 calls, while a loop or a cached version takes 30 steps. Recursion that doesn't repeat work, like a tree traversal, is fine.