Skip to Content
Course content

77: Recursion in Practice

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine you're tasked with finding a specific golden ticket hidden inside a series of nesting boxes. You open the first large box, but instead of the ticket, you find three smaller boxes. You don't know which one holds the ticket, or if the ticket is inside a box that's inside one of those smaller boxes. So, what do you do? You pick one of the smaller boxes and repeat the exact same process: open it, check for the ticket, and if you find more boxes, open those too.

That process—applying the same logic to a smaller version of the original problem—is recursion in a nutshell. In Python, we do this by having a function call itself. Here is how that mental model maps to the code we're about to write:

  • Opening a box is the function call.
  • Finding the ticket (or an empty box) is your "base case." It's the signal to stop searching and start heading back.
  • Finding more boxes is the recursive step. You're diving deeper into the structure.

Untangling Nested Lists

In the real world, you'll rarely use recursion to calculate a factorial (that's just for textbooks). You'll use it when you're dealing with data that has an unknown depth—like a file system with folders inside folders, or a JSON response from an API that nests objects unpredictably. Let's look at a practical example: flattening a deeply nested list.

I've often seen developers try to solve this with a for loop, but then they realize their data is nested three levels deep, and they add another loop. Then the data comes back four levels deep, and the code breaks. Recursion handles this regardless of the depth.

def flatten_list(nested_data):
    flat_list = []
    
    for item in nested_data:
        if isinstance(item, list):
            # We found another 'box'! Let's dive in.
            # We call the function again and extend our results.
            flat_list.extend(flatten_list(item))
        else:
            # We found the 'ticket' (an actual element).
            flat_list.append(item)
            
    return flat_list

# Let's try it with a messy, unevenly nested list
messy_data = [1, [2, [3, 4], 5], 6, [7, 8]]
print(flatten_list(messy_data)) 
# Output: [1, 2, 3, 4, 5, 6, 7, 8]

Knowing When to Stop

The biggest danger with recursion is the infinite loop—or as Python calls it, the RecursionError. This happens when you forget your base case. In the example above, the base case is implicit: the loop eventually runs out of items in the list, and the function returns. If the function just called itself without ever reaching a point where it stopped, Python would kill the process to save your memory.

I'll be honest with you: recursion can feel like a brain-bender at first. The trick is to stop trying to trace every single jump the computer makes. Instead, trust the base case and trust that the function works for one "level" of the problem. If it works for one level, the recursion will handle the rest.




📋 Practical Task

The Nested Comment Thread Flattener

You are building a simplified version of a Reddit-style comment system. Comments can have "replies," and those replies can have their own replies, creating a tree-like structure. Your goal is to write a recursive function that takes a nested list of comments and returns a single, flat list of all the comment texts in the order they appear.

The Data Structure: Each comment is represented as a list where the first element is the text (a string) and the second element is a list of replies (which are also comments).

comments_tree = [
    ["Great post!", [
        ["I disagree with point A", [
            ["Actually, point A is well-documented", []]
        ]],
        ["Thanks for sharing!", []]
    ]],
    ["Very helpful, thanks!", [
        ["Glad you liked it!", []]
    ]]
]

Your Task: Write a function extract_all_comments(tree) that traverses this structure and returns a flat list of strings: ["Great post!", "I disagree with point A", "Actually, point A is well-documented", "Thanks for sharing!", "Very helpful, thanks!", "Glad you liked it!"].

Rating
0 0

There are no comments for now.

to be the first to leave a comment.