Python (Fall 2026) Module 13-4: Recursive Tree Traversal
Watch on YouTube →
Overview
Bill Moseley demonstrates Python recursion by building `processList`, a function that uses `isinstance(item, list)` to distinguish nested lists from individual values. The function counts leaf items, recursively processes sublists of any depth, and adds each sublist’s returned count to produce one total, such as five fruit items or ten names in nested lists.
Key takeaways
- In Python, `isinstance(item, list)` lets `processList` distinguish a nested list from a leaf value and choose whether to recurse or increment the count.
- A recursive count must add each sublist’s returned value to the parent’s accumulator with `intCount += processList(item)`.
- Returning after the loop is essential: returning from inside the loop can stop a call before it has processed every item in its list.
- The recursive approach counts leaf values across lists of unknown depth without needing a separate traversal rule for each nesting level.
- Debug print statements showing each item’s count and each call’s return value make the flow of nested recursive calls easier to follow.
Chapters
- Moseley initializes `intCount` to zero inside `processList` and iterates through each item in `liData`.
- The condition `not isinstance(item, list)` identifies individual values, which are counted with `intCount += 1`.
- Testing the function on a flat fruit list containing apple, orange, and pear returns a count of three.
- A nested fruit list exposes the limitation of counting only non-list items: the sublist itself is not counted or traversed.
- The recursive case calls `processList(item)` when an item is a list, allowing the same logic to handle its contents.
- Returning a count from each call and adding it to the parent with `intCount += processList(item)` produces one total instead of separate sublist counts.
- The return statement must come after the loop so each call finishes processing all items before sending its total back.
- Moseley adds print statements to show item counts and returns; a sublist containing grapes and kiwi returns two, which contributes to the overall count of five.
- The same `processList` function counts a nested people list with multiple levels, returning ten items across its branches.
- Because each recursive call handles another list, the algorithm can process structures with added levels without specifying their depth in advance.
- Moseley describes recursion as difficult to track, while noting its usefulness for structures such as nested lists and fractal patterns like the dragon curve.
Transcript excerpt
0:01 All right. So, let's use this pseudo code to see what we have here. So for each item in the list, for item in liData, if it's a single item, now this one is, I'm going to give you a new function here. If isinstance.
0:49 And what we're going to say here is we're going to write item, because we're going to check and see if the item is something in particular. And then we're going to give it the type of item we're looking for. So this is a new function, isinstance, but basically we're, what we're saying is, is, is that item a list? Is it a list? And of course we want to find out if it's a single item. So we want to know if it's not a list. So we're going to say not in front of that. So if that item is not a list,
1:45 then we want to count it. How do we count that? We need to, we need a variable. So let's say intCount = 0. intCount = 0. I'll make this camel case. intCount = 0. And down here when we count that, intCount += 1. So if we give the, the list to that processList, it's going to start a counter internal, that internal to that. Remember, what happens in the function stays in the function. It's going to start counting at
2:34 zero and it's going to start going through each item in that list, and it's going to say if it's not a list, if that item is not a list, then count it. Then add one to the counter. Now before I do the otherwise part, which is the recursion, I'm going to just, down here at the very end, I'm going to print, and that's going to be, even that's indented at the same level as the
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.