Trey Hunner 🐍 on Nostr: Python Tip #130 (of 365): Only use recursion for trees Recursion is most useful for ...
Python Tip #130 (of 365):
Only use recursion for trees
Recursion is most useful for traversing or constructing TREE-LIKE STRUCTURES.
For most other problems, iteration is usually more readable.
Recursion is when a function calls itself.
Computer science classes almost always introduce recursion using factorial or fibonacci. Those are not great examples because those are both very clear (and fast) using a simple "for" loop instead.
🧵 (1/6)
#Python #DailyPythonTip
Published at
2026-05-10 19:46:00 CESTEvent JSON
{
"id": "be4ed60f6d65f30386979623995f95b26fd1b9bd8080a9c3293a1a00ed770e46",
"pubkey": "7e9159fbfd5526415c8d2de2e4c7654e3660883f9ccae720a70fa3c1f3cf1f45",
"created_at": 1778435160,
"kind": 1,
"tags": [
[
"t",
"dailypythontip"
],
[
"t",
"python"
],
[
"proxy",
"https://mastodon.social/@treyhunner/116551526695502692",
"web"
],
[
"proxy",
"https://mastodon.social/users/treyhunner/statuses/116551526695502692",
"activitypub"
],
[
"L",
"pink.momostr"
],
[
"l",
"pink.momostr.activitypub:https://mastodon.social/users/treyhunner/statuses/116551526695502692",
"pink.momostr"
],
[
"-"
]
],
"content": "Python Tip #130 (of 365):\n\nOnly use recursion for trees\n\nRecursion is most useful for traversing or constructing TREE-LIKE STRUCTURES.\n\nFor most other problems, iteration is usually more readable.\n\nRecursion is when a function calls itself.\n\nComputer science classes almost always introduce recursion using factorial or fibonacci. Those are not great examples because those are both very clear (and fast) using a simple \"for\" loop instead.\n\n🧵 (1/6)\n\n#Python #DailyPythonTip",
"sig": "3b61d44e7c76824b00310e0f433d2ef244bf174f7b6665adaeff38afbfed8ddc9d3a81542405da7ae0299ceae1951f111f101adfeeb01321aab497807c7715e0"
}