Find Depth of List in Python (2 Examples)
Hi! This tutorial will show you how to determine the depth of a list in the Python programming language.
Here is an overview:
Let’s get into the Python code!
Create Example List
Here, we will create a nested Python list of integers whose deepest level we will ascertain in this tutorial.
So, in your Python programming IDE, run the code below to create the example nested list:
my_list = [[1,2,3],[4,5,6],[7,8,9]] print(my_list) # [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(type(my_list)) # <class 'list'>
Now that we have created the example nested list. Let us examine two ways to get the depth of the list.
Example 1: Get Depth of List Using Function Recursion
In this first example, we will use function recursion to determine the depth of the nested list my_list
:
def get_list_depth(lst): if isinstance(lst, list): return 1 + max(get_list_depth(item) for item in lst) else: return 0 depth = get_list_depth(my_list) print(depth) # 2
Here, we first check if my_list
is an instance of a list using the isinstance() function.
If it is a list, the function recursively calls itself for each item in my_list
using a generator expression, and returns the maximum depth among those items adding 1
to account for the current level.
If my_list
is not a list, the function returns 0
, indicating that it is not nested.
The resulting depth is then assigned to the variable depth
, and it is printed to the console using the print() function.
Example 2: Get Depth of List Using Iterative Approach
In this next example, we will use an iterative approach to know the depth of the nested list:
depth = 0 stack = [(my_list, 1)] while stack: curr_list, curr_depth = stack.pop() if isinstance(curr_list, list): depth = max(depth, curr_depth) for item in curr_list: stack.append((item, curr_depth + 1)) print(depth) # 2
In the above example, we first initialize a variable depth
to 0
and create a stack with a tuple (my_list
, 1
) as the initial element.
Then, a while loop runs until the stack is empty.
In each iteration, it pops the top item from the stack and assigns it to curr_list
and curr_depth
.
If curr_list
is a list, depth
is updated to the maximum value between the current depth (which is held in depth) and curr_depth
.
Then, it iterates over each item in curr_list
and pushes them onto the stack along with an incremented depth.
Once the loop finishes, the final value of depth is printed to the console.
Video, Further Resources & Summary
Do you need more explanations on how to find the depth of a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to find the depth of a list in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Transpose 2D List in Python (3 Examples)
- Append to 2D List in Python (3 Examples)
- What is List Node in Python? (2 Examples)
- count() Method for Lists in Python (2 Examples)
- Introduction to Python Programming
This post has shown, using two examples, how to find the depth of a list in Python. Your use case will typically determine which solution to adopt.
I do hope you found this tutorial helpful! In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter