Save this video free

Python (Fall 2026) Module 13-4: Recursive Tree Traversal

Bill Moseley, Ph.D. - Computer Science Educator · 18:54 · Watch on YouTube

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

Chapters

0:00 Count Individual Items with Python’s isinstance
6:00 Use Recursive Calls to Process Nested Lists
13:00 Trace Recursive Returns Across Lists of Any Depth

Keep these chapters and the full searchable transcript in your own library.

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.

Want the full transcript?

Save this video in YouTube Collector to get its complete searchable transcript, your own AI summaries, and a library that keeps every video you collect in one place.