If you’re familiar with functions in Python, then you know that it’s quite common for one function to call another. In Python, it’s also possible for a function to call itself! A function that calls itself is said to be recursive, and the technique of employing a recursive function is called recursion. Show
It may seem peculiar for a function to call itself, but many types of programming problems are best expressed recursively. When you bump up against such a problem, recursion is an indispensable tool for you to have in your toolkit. By the end of this tutorial, you’ll understand:
Then you’ll study several Python programming problems that use recursion and contrast the recursive solution with a comparable non-recursive one. Free Bonus: Get a sample chapter from Python Basics: A Practical Introduction to Python 3 to see how you can go from beginner to intermediate in Python with a complete curriculum, up to date for Python 3.9. What Is Recursion?The word recursion comes from the Latin word recurrere, meaning to run or hasten back, return, revert, or recur. Here are some online definitions of recursion:
A recursive definition is one in which the defined term appears in the definition itself. Self-referential situations often crop up in real life, even if they aren’t immediately recognizable as such. For example, suppose you wanted to describe the set of people that make up your ancestors. You could describe them this way: Notice how the concept that is being defined, ancestors, shows up in its own definition. This is a recursive definition. In programming, recursion has a very precise meaning. It refers to a coding technique in which a function calls itself. Remove adsWhy Use Recursion?Most programming problems are solvable without recursion. So, strictly speaking, recursion usually isn’t necessary. However, some situations particularly lend themselves to a self-referential definition—for example, the definition of ancestors shown above. If you were devising an algorithm to handle such a case programmatically, a recursive solution would likely be cleaner and more concise. Traversal of tree-like data structures is another good example. Because these are nested structures, they readily fit a recursive definition. A non-recursive algorithm to walk through a nested structure is likely to be somewhat clunky, while a recursive solution will be relatively elegant. An example of this appears later in this tutorial. On the other hand, recursion isn’t for every situation. Here are some other factors to consider:
Typically, the readability of the code will be the biggest determining factor. But it depends on the circumstances. The examples presented below should help you get a feel for when you should choose recursion. Recursion in PythonWhen you call a function in Python, the interpreter creates a new local namespace so that names defined within that function don’t collide with identical names defined elsewhere. One function can call another, and even if they both define objects with the same name, it all works out fine because those objects exist in separate namespaces. The same holds true if multiple instances of the same function are running concurrently. For example, consider the following definition:
When 4 executes the first time, Python creates a namespace and assigns 5 the value 6 in that namespace. Then 4 calls itself recursively. The second time 4 runs, the interpreter creates a second namespace and assigns 6 to 5 there as well. These two instances of the name 5 are distinct from each another and can coexist without clashing because they are in separate namespaces.Unfortunately, running 4 as it stands produces a result that is less than inspiring, as the following traceback shows:>>>
As written, 4 would in theory go on forever, calling itself over and over without any of the calls ever returning. In practice, of course, nothing is truly forever. Your computer only has so much memory, and it would run out eventually.Python doesn’t allow that to happen. The interpreter limits the maximum number of times a function can call itself recursively, and when it reaches that limit, it raises a 4 exception, as you see above.Technical note: You can find out what Python’s recursion limit is with a function from the 5 module called 6:>>>
You can change it, too, with 7:>>>
You can set it to be pretty large, but you can’t make it infinite. There isn’t much use for a function to indiscriminately call itself recursively without end. It’s reminiscent of the instructions that you sometimes find on shampoo bottles: “Lather, rinse, repeat.” If you were to follow these instructions literally, you’d shampoo your hair forever! This logical flaw has evidently occurred to some shampoo manufacturers, because some shampoo bottles instead say “Lather, rinse, repeat as necessary.” That provides a termination condition to the instructions. Presumably, you’ll eventually feel your hair is sufficiently clean to consider additional repetitions unnecessary. Shampooing can then stop. Similarly, a function that calls itself recursively must have a plan to eventually stop. Recursive functions typically follow this pattern:
You’re now ready to see how this works with some examples. Remove adsGet Started: Count Down to ZeroThe first example is a function called 8, which takes a positive number as an argument and prints the numbers from the specified argument down to zero:>>>
Notice how 8 fits the paradigm for a recursive algorithm described above:
Note: For simplicity, 8 doesn’t check its argument for validity. If 0 is either a non-integer or negative, you’ll get a 4 exception because the base case is never reached.The version of 8 shown above clearly highlights the base case and the recursive call, but there’s a more concise way to express it:
Here’s one possible non-recursive implementation for comparison: >>>
This is a case where the non-recursive solution is at least as clear and intuitive as the recursive one, and probably more so. Calculate FactorialThe next example involves the mathematical concept of factorial. The factorial of a positive integer n, denoted as n!, is defined as follows: In other words, n! is the product of all integers from 1 to n, inclusive. Factorial so lends itself to recursive definition that programming texts nearly always include it as one of the first examples. You can express the definition of n! recursively like this: As with the example shown above, there are base cases that are solvable without recursion. The more complicated cases are reductive, meaning that they reduce to one of the base cases:
For example, recursive computation of 4! looks like this: Recursive Calculation of 4!The calculations of 4!, 3!, and 2! suspend until the algorithm reaches the base case where n = 1. At that point, 1! is computable without further recursion, and the deferred calculations run to completion. Remove adsDefine a Python Factorial FunctionHere’s a recursive Python function to calculate factorial. Note how concise it is and how well it mirrors the definition shown above: >>>
A little embellishment of this function with some 6 statements gives a clearer idea of the call and return sequence:>>>
Notice how all the recursive calls stack up. The function gets called with 0 = 8, 9, 0, and 1 in succession before any of the calls return. Finally, when 0 is 1, the problem can be solved without any more recursion. Then each of the stacked-up recursive calls unwinds back out, returning 1, 0, 6, and finally 7 from the outermost call.Recursion isn’t necessary here. You could implement 8 iteratively using a 9 loop:>>>
You can also implement factorial using Python’s 0, which you can import from the 1 module:>>> 0Again, this shows that if a problem is solvable with recursion, there will also likely be several viable non-recursive solutions as well. You’ll typically choose based on which one results in the most readable and intuitive code. Another factor to take into consideration is execution speed. There can be significant performance differences between recursive and non-recursive solutions. In the next section, you’ll explore these differences a little further. Speed Comparison of Factorial ImplementationsTo evaluate execution time, you can use a function called from a module that is also called 3. This function supports a number of different formats, but you’ll use the following format in this tutorial: 1 2 first executes the commands contained in the specified 5. Then it executes 6 the given number of 7 and reports the cumulative execution time in seconds:>>> 2Here, the 8 parameter assigns 9 the value 0. Then 2 prints 9 one hundred times. The total execution time is just over 3/100 of a second.The examples shown below use 2 to compare the recursive, iterative, and 0 implementations of factorial from above. In each case, 5 contains a setup string that defines the relevant 8 function. 2 then executes 8 a total of ten million times and reports the aggregate execution.First, here’s the recursive version: >>> 3Next up is the iterative implementation: >>> 4Last, here’s the version that uses 0:>>> 5In this case, the iterative implementation is the fastest, although the recursive solution isn’t far behind. The method using 0 is the slowest. Your mileage will probably vary if you try these examples on your own machine. You certainly won’t get the same times, and you may not even get the same ranking.Does it matter? There’s a difference of almost four seconds in execution time between the iterative implementation and the one that uses 0, but it took ten million calls to see it.If you’ll be calling a function many times, you might need to take execution speed into account when choosing an implementation. On the other hand, if the function will run relatively infrequently, then the difference in execution times will probably be negligible. In that case, you’d be better off choosing the implementation that seems to express the solution to the problem most clearly. For factorial, the timings recorded above suggest a recursive implementation is a reasonable choice. Frankly, if you’re coding in Python, you don’t need to implement a factorial function at all. It’s already available in the standard 2 module:>>> 6Perhaps it might interest you to know how this performs in the timing test: >>> 7Wow! 3 performs better than the best of the other three implementations shown above by roughly a factor of 10.Technical note: The fact that 3 is so much speedier probably has nothing to do with whether it’s implemented recursively. More likely it’s because the function is implemented in C rather than Python. For more reading on Python and C, see these resources:
A function implemented in C will virtually always be faster than a corresponding function implemented in pure Python. Remove adsTraverse a Nested ListThe next example involves visiting each item in a nested list structure. Consider the following Python : 8As the following diagram shows, 5 contains two sublists. The first of these sublists itself contains another sublist:Suppose you wanted to count the number of leaf elements in this list—the lowest-level 6 objects—as though you’d flattened out the list. The leaf elements are 7, 8, 9, 00, 01, 02, 03, 04, 05, and 06, so the answer should be 6.Just calling 08 on the list doesn’t give the correct answer:>>> 9 08 counts the objects at the top level of 5, which are the three leaf elements 7, 03, and 06 and two sublists 14 and 15:>>> 0What you need here is a function that traverses the entire list structure, sublists included. The algorithm goes something like this:
Note the self-referential nature of this description: Walk through the list. If you encounter a sublist, then similarly walk through that list. This situation begs for recursion! Traverse a Nested List RecursivelyRecursion fits this problem very nicely. To solve it, you need to be able to determine whether a given list item is leaf item or not. For that, you can use the built-in Python function . In the case of the 5 list, if an item is an instance of type 18, then it’s a sublist. Otherwise, it’s a leaf item:>>> 1Now you have the tools in place to implement a function that counts leaf elements in a list, accounting for sublists recursively: 2If you run 19 on several lists, including the 5 list defined above, you get this:>>> 3As with the factorial example, adding some 6 statements helps to demonstrate the sequence of recursive calls and return values: 4Here’s a synopsis of what’s happening in the example above:
Note: To keep things simple, this implementation assumes the list passed to 19 contains only leaf items or sublists, not any other type of composite object like a dictionary or .The output from 19 when it’s executed on the 5 list now looks like this:>>> 5Each time a call to 19 terminates, it returns the count of leaf elements it tallied in the list passed to it. The top-level call returns 6, as it should.Remove adsTraverse a Nested List Non-RecursivelyLike the other examples shown so far, this list traversal doesn’t require recursion. You can also accomplish it iteratively. Here’s one possibility: 6If you run this non-recursive version of 19 on the same lists as shown previously, you get the same results:>>> 3The strategy employed here uses a stack to handle the nested sublists. When this version of 19 encounters a sublist, it pushes the list that is currently in progress and the current index in that list onto a stack. Once it has counted the sublist, the function pops the parent list and index from the stack so it can resume counting where it left off.In fact, essentially the same thing happens in the recursive implementation as well. When you call a function recursively, Python saves the state of the executing instance on a stack so the recursive call can run. When the recursive call finishes, the state is popped from the stack so that the interrupted instance can resume. It’s the same concept, but with the recursive solution, Python is doing the state-saving work for you. Notice how concise and readable the recursive code is when compared to the non-recursive version: Recursive vs Non-Recursive Nested List TraversalThis is a case where using recursion is definitely an advantage. Detect PalindromesThe choice of whether to use recursion to solve a problem depends in large part on the nature of the problem. Factorial, for example, naturally translates to a recursive implementation, but the iterative solution is quite straightforward as well. In that case, it’s arguably a toss-up. The list traversal problem is a different story. In that case, the recursive solution is very elegant, while the non-recursive one is cumbersome at best. For the next problem, using recursion is arguably silly. A palindrome is a word that reads the same backward as it does forward. Examples include the following words:
If asked to devise an algorithm to determine whether a string is palindromic, you would probably come up with something like “Reverse the string and see if it’s the same as the original.” You can’t get much plainer than that. Even more helpfully, Python’s 35 slicing syntax for reversing a string provides a convenient way to code it:>>> 8This is clear and concise. There’s hardly any need to look for an alternative. But just for fun, consider this recursive definition of a palindrome:
Slicing is your friend here as well. For a string 36, indexing and slicing give the following substrings:
So you can define 40 recursively like this:>>> 9It’s an interesting exercise to think recursively, even when it isn’t especially necessary. Remove adsSort With QuicksortThe final example presented, like the nested list traversal, is a good example of a problem that very naturally suggests a recursive approach. The is an efficient sorting algorithm developed by British computer scientist Tony Hoare in 1959. Quicksort is a divide-and-conquer algorithm. Suppose you have a list of objects to sort. You start by choosing an item in the list, called the pivot item. This can be any item in the list. You then partition the list into two sublists based on the pivot item and recursively sort the sublists. The steps of the algorithm are as follows:
Each partitioning produces smaller sublists, so the algorithm is reductive. The base cases occur when the sublists are either empty or have one element, as these are inherently sorted. Choosing the Pivot ItemThe Quicksort algorithm will work no matter what item in the list is the pivot item. But some choices are better than others. Remember that when partitioning, two sublists that are created: one with items that are less than the pivot item and one with items that are greater than the pivot item. Ideally, the two sublists are of roughly equal length. Imagine that your initial list to sort contains eight items. If each partitioning results in sublists of roughly equal length, then you can reach the base cases in three steps: Optimal Partitioning, Eight-Item ListAt the other end of the spectrum, if your choice of pivot item is especially unlucky, each partition results in one sublist that contains all the original items except the pivot item and another sublist that is empty. In that case, it takes seven steps to reduce the list to the base cases: Suboptimal Partitioning, Eight-Item ListThe Quicksort algorithm will be more efficient in the first case. But you’d need to know something in advance about the nature of the data you’re sorting in order to systematically choose optimal pivot items. In any case, there isn’t any one choice that will be the best for all cases. So if you’re writing a Quicksort function to handle the general case, the choice of pivot item is somewhat arbitrary. The first item in the list is a common choice, as is the last item. These will work fine if the data in the list is fairly randomly distributed. However, if the data is already sorted, or even nearly so, then these will result in suboptimal partitioning like that shown above. To avoid this, some Quicksort algorithms choose the middle item in the list as the pivot item. Another option is to find the median of the first, last, and middle items in the list and use that as the pivot item. This is the strategy used in the sample code below. Implementing the PartitioningOnce you’ve chosen the pivot item, the next step is to partition the list. Again, the goal is to create two sublists, one containing the items that are less than the pivot item and the other containing those that are greater. You could accomplish this directly in place. In other words, by swapping items, you could shuffle the items in the list around until the pivot item is in the middle, all the lesser items are to its left, and all the greater items are to its right. Then, when you Quicksort the sublists recursively, you’d pass the slices of the list to the left and right of the pivot item. Alternately, you can use Python’s list manipulation capability to create new lists instead of operating on the original list in place. This is the approach taken in the code below. The algorithm is as follows:
Note that this involves creating a third sublist that contains the pivot item itself. One advantage to this approach is that it smoothly handles the case where the pivot item appears in the list more than once. In that case, list 2 will have more than one element. Remove adsUsing the Quicksort ImplementationNow that the groundwork is in place, you are ready to move on to the Quicksort algorithm. Here’s the Python code: 0This is what each section of 41 is doing:
Note: This example has the advantage of being succinct and relatively readable. However, it isn’t the most efficient implementation. In particular, the creation of the partition lists on lines 14 to 18 involves iterating through the list three separate times, which isn’t optimal from the standpoint of execution time. Here are some examples of 41 in action:>>> 1For testing purposes, you can define a short function that generates a list of random numbers between 1 and 44: 2Now you can use 45 to test 41:>>> 3To further understand how 41 works, see the diagram below. This shows the recursion sequence when sorting a twelve-element list:Quicksort Algorithm, Twelve-Element ListIn the first step, the first, middle, and last list values are 48, 49, and 50, respectively. The median is 48, so that becomes the pivot item. The first partition then consists of the following sublists:SublistItems 52The items less than the pivot item 53The pivot item itself 54The items greater than the pivot itemEach sublist is subsequently partitioned recursively in the same manner until all the sublists either contain a single element or are empty. As the recursive calls return, the lists are reassembled in sorted order. Note that in the second-to-last step on the left, the pivot item 55 appears in the list twice, so the pivot item list has two elements.ConclusionThat concludes your journey through recursion, a programming technique in which a function calls itself. Recursion isn’t by any means appropriate for every task. But some programming problems virtually cry out for it. In those situations, it’s a great technique to have at your disposal. In this tutorial, you learned:
You also saw several examples of recursive algorithms and compared them to corresponding non-recursive solutions. You should now be in a good position to recognize when recursion is called for and be ready to use it confidently when it’s needed! If you want to explore more about recursion in Python, then check out Thinking Recursively in Python. Mark as Completed 🐍 Python Tricks 💌 Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team. Send Me Python Tricks » About John Sturtz John is an avid Pythonista and a member of the Real Python tutorial team. » More about JohnEach tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are: Aldren Bartosz Joanna Jacob Master Real-World Python Skills With Unlimited Access to Real Python Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas: Level Up Your Python Skills » Master Real-World Python Skills Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas: Level Up Your Python Skills » What Do You Think? Rate this article: Tweet Share Share EmailWhat’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know. Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. and get answers to common questions in our support portal. |