Python Program to Flatten Nested List Using Recursion
def flatten(lst): return [item for sublist in lst for item in (flatten(sublist) if isinstance(sublist, list) else [sublist])].Examples
How to Think About It
Algorithm
Code
def flatten(lst): result = [] for item in lst: if isinstance(item, list): result.extend(flatten(item)) else: result.append(item) return result nested_list = [1, [2, 3], [4, [5, 6]], 7] print(flatten(nested_list))
Dry Run
Let's trace the list [1, [2, 3], [4, [5, 6]], 7] through the flatten function.
Start with the full list
lst = [1, [2, 3], [4, [5, 6]], 7], result = []
Process first item 1
1 is not a list, append 1 to result -> result = [1]
Process second item [2, 3]
[2, 3] is a list, call flatten([2, 3])
Flatten [2, 3]
Both 2 and 3 are not lists, return [2, 3]
Extend result with [2, 3]
result = [1, 2, 3]
Process third item [4, [5, 6]]
[4, [5, 6]] is a list, call flatten([4, [5, 6]])
Flatten [4, [5, 6]]
4 is not a list, append 4; [5, 6] is a list, call flatten([5, 6])
Flatten [5, 6]
5 and 6 are not lists, return [5, 6]
Combine results
return [4, 5, 6]
Extend main result
result = [1, 2, 3, 4, 5, 6]
Process last item 7
7 is not a list, append 7 -> result = [1, 2, 3, 4, 5, 6, 7]
Return final result
[1, 2, 3, 4, 5, 6, 7]
| Step | Current Item | Is List? | Result List |
|---|---|---|---|
| 1 | 1 | No | [1] |
| 2 | [2, 3] | Yes | [1, 2, 3] |
| 3 | [4, [5, 6]] | Yes | [1, 2, 3, 4, 5, 6] |
| 4 | 7 | No | [1, 2, 3, 4, 5, 6, 7] |
Why This Works
Step 1: Check if item is a list
The function uses isinstance(item, list) to decide if it needs to open a nested list or just add the item.
Step 2: Use recursion to flatten nested lists
If the item is a list, the function calls itself to flatten that smaller list before adding its items.
Step 3: Collect all items in one list
All non-list items and flattened sublist items are added to a single result list, removing all nesting.
Alternative Approaches
def flatten_gen(lst): for item in lst: if isinstance(item, list): yield from flatten_gen(item) else: yield item nested_list = [1, [2, 3], [4, [5, 6]], 7] print(list(flatten_gen(nested_list)))
def flatten_iter(lst): stack = lst[::-1] result = [] while stack: item = stack.pop() if isinstance(item, list): stack.extend(item[::-1]) else: result.append(item) return result nested_list = [1, [2, 3], [4, [5, 6]], 7] print(flatten_iter(nested_list))
Complexity: O(n) time, O(n) space
Time Complexity
The function visits every element once, including all nested elements, so the time grows linearly with the total number of elements.
Space Complexity
The extra space is used for the result list and the recursion call stack, both proportional to the total number of elements.
Which Approach is Fastest?
The recursive and iterative methods have similar time complexity, but the iterative approach avoids recursion limits and may be faster for very deep lists.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Recursive | O(n) | O(n) | Simple code, moderate depth |
| Generator with yield | O(n) | O(n) | Memory efficient, lazy evaluation |
| Iterative with stack | O(n) | O(n) | Very deep or large nested lists |