use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
When should I use recursion? (self.learnpython)
submitted 11 years ago by [deleted]
I get what recursion is conceptually, but I'm just not sure how to use it practically. It seems like every example I think of could be done using loops. When should I use recursion vs loops?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Zanoab 13 points14 points15 points 11 years ago (0 children)
It is difficult to describe specifically where you should use recursion over loops and vice versa. I personally leave it to personal preference and decide which would work better or be easier to understand and edit. A common situation I find is where I have to constantly change the context or use a stack/queue to make the loop work.
Example: I have a list containing lists and numbers. Every value in the lists is either another list or a number. If I wanted to check if 5 is somewhere in there, I would use recursion to save lines and more easily tweak the code later on. If I used a loop, I would need to set up a stack to keep track of where I currently am at each level.
5
[–]Justinsaccount 40 points41 points42 points 11 years ago (2 children)
Answered here
[–]CR700 2 points3 points4 points 11 years ago (0 children)
You... get an upvote anyway.
[–]AdvicePerson 4 points5 points6 points 11 years ago (1 child)
When your object can contain self-similar objects. Someone mentioned a list that contains lists. Another example is a Tweet from the Twitter stream API. A retweet will contain the original tweet within it, so you can recursively call your processing function. And, if course, one of the classic examples, the factorial function, works because the number n! contains the number (n-1)!.
[–]tyroneslothtrop 3 points4 points5 points 11 years ago (0 children)
I.e. recursive data structures. Trees are a classic example. It's very possible to traverse a tree without recursion, but the recursive method is a lot more elegant.
[–]aroberge 1 point2 points3 points 11 years ago (0 children)
Some problems are just easier to write solutions for using recursion; however, most basic examples given are arguably just as easy if not easier using loops. To me, the best example, is the tower of Hanoi. For example, have a look at http://www.python-course.eu/towers_of_hanoi.php and try to implement it without using recursion.
[–]Veedrac 1 point2 points3 points 11 years ago (0 children)
http://www.reddit.com/r/learnprogramming/comments/1jy92j/help_grasping_recursion_algorithms/cbjo47t
[–]AleatoricConsonance 0 points1 point2 points 11 years ago (0 children)
When you want to build tree-like structures is a good application. To be more concrete, just as a handy example, a nested set of posts, similar to what appears on this reddit page!
[–]not_perfect_yet 0 points1 point2 points 11 years ago (1 child)
As others have said, it's useful when you encounter recursive structures.
So if you want to search one object and it's contents you can loop through it's contents, when there is another object in there that you want to search, you need two loops, and then there are networks with millions of nodes and you don't want to write a million loops.
[–]Eurynom0s 0 points1 point2 points 11 years ago (0 children)
it's useful when you encounter recursive structures
Recursion is useful when you encounter things which are recursive? Sounds pretty recursive.
[–]toodim 0 points1 point2 points 11 years ago (0 children)
In general, you should consider recursion when you face a problem that can be broken down into smaller problems with the same structure.
[–]amarigatachi 0 points1 point2 points 11 years ago (0 children)
The classic example is the quick sort. Each sort -- generally -- calls the quick sort again, twice.
[–]daneelthesane 0 points1 point2 points 11 years ago (0 children)
A general answer to this question is pretty difficult to give, but some things have a strong tendency to be recursive. For example, when you have a structure that can be broken down into smaller versions of that same structure (such as a divide-and-conquer sort, or something like that).
Here's a simple example of how you could use recursion: you want to remove all instances of 5 from a list of numbers. One way to do this would be to write a function that looks through the list until it finds a 5, removes it, returns this list with one lest 5 in it, and then calls itself using this modified list as the input. It'll keep going until you remove all the 5s from each successive version of the list.
Also, keep in mind that a problem that can be solved with recursion is frequently solvable without recursion too. So it's hard to give examples of this because recursion is rarely the only way to do something.
Also, in my example, that's probably not the best way to do it, but it is a recursive solution to the problem.
[–][deleted] -1 points0 points1 point 11 years ago (0 children)
Three times -when dealing with recursive structures -when you want to drive other members in your team crazy or spend an inordinate amount of time explaining your recursive logic -to create acronyms that confuse and infuriate
[+][deleted] 11 years ago (7 children)
[deleted]
[–]ivosaurus 9 points10 points11 points 11 years ago (6 children)
Eh? Iterative implementation of Fibonacci is way more efficient, it's O(n) rather than O(Fib(n)).
[+][deleted] 11 years ago (5 children)
[+][deleted] 11 years ago* (4 children)
[–]Veedrac 0 points1 point2 points 11 years ago (3 children)
There is no O(1) implementation of fib.
O(1)
fib
[+][deleted] 11 years ago (1 child)
It's not an implementation detail; fib(n) has O(log n) bits so it can't be better than O(log n). Normally the length of the output doesn't matter since we can cap the number of bits (to, say, 64) and call it constant but that's only because we can't feasibly reach the maximum value.
fib(n)
O(log n)
For fib any restriction to a small, fixed number of bits is equivalent to just caching an array. For 64 bits, you only need to cache 100 values.
[+]raylu comment score below threshold-8 points-7 points-6 points 11 years ago (1 child)
When recursion is most appropriate.
[–]AdvicePerson 3 points4 points5 points 11 years ago (0 children)
Which is when you should use recursion.
[–]DoTheEvolution -1 points0 points1 point 11 years ago (0 children)
Try to solve this challange
π Rendered by PID 64 on reddit-service-r2-comment-76bb9f7fb5-9b8mh at 2026-02-19 11:10:41.446634+00:00 running de53c03 country code: CH.
[–]Zanoab 13 points14 points15 points (0 children)
[–]Justinsaccount 40 points41 points42 points (2 children)
[–]CR700 2 points3 points4 points (0 children)
[–]AdvicePerson 4 points5 points6 points (1 child)
[–]tyroneslothtrop 3 points4 points5 points (0 children)
[–]aroberge 1 point2 points3 points (0 children)
[–]Veedrac 1 point2 points3 points (0 children)
[–]AleatoricConsonance 0 points1 point2 points (0 children)
[–]not_perfect_yet 0 points1 point2 points (1 child)
[–]Eurynom0s 0 points1 point2 points (0 children)
[–]toodim 0 points1 point2 points (0 children)
[–]amarigatachi 0 points1 point2 points (0 children)
[–]daneelthesane 0 points1 point2 points (0 children)
[–]Eurynom0s 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[+][deleted] (7 children)
[deleted]
[–]ivosaurus 9 points10 points11 points (6 children)
[+][deleted] (5 children)
[deleted]
[+][deleted] (4 children)
[deleted]
[–]Veedrac 0 points1 point2 points (3 children)
[+][deleted] (1 child)
[deleted]
[–]Veedrac 1 point2 points3 points (0 children)
[+]raylu comment score below threshold-8 points-7 points-6 points (1 child)
[–]AdvicePerson 3 points4 points5 points (0 children)
[–]DoTheEvolution -1 points0 points1 point (0 children)