π€Deep Lists
Visualizing lists.
Last updated
Visualizing lists.
Last updated
Previously, we mentioned that a list is a collection of elements. Now, we develop this concept with a question β can a list be an element? The answer is yes! Something like the following is extremely possible in Python:
The above is a list where the first element is 1
, the second element is [2, 3]
and the third element is [4, 5, 6]
. Let's also take a moment to look at the visualization, as generated by PythonTutor:
Wait, what are these arrows? These arrows are pointers to the list that we created. Here, we tackle one of the most challenging concepts of this class: The variable lst
is not the list itself but a pointer to that list. Similarly, the first element of the list is not the list [2, 3]
but rather a pointer to the list [2, 3]
.
Lists that contain other lists are known as "deep lists". Conversely, lists without any other iterables as elements are known as "shallow lists". You'll see these phrases popping up time and again in this book and in 61A βΒ some functions on lists are "shallow", in that they only execute on the list passed in as the argument and not any lists within it. Similarly, some functions are "deep", in that they execute on the list passed in as the argument, and every list within it. The distinction is critical!
While this concept might not feel very important right now, it is going to get particularly interesting as we further our understanding of lists. Keep this in the back of your mind as you tackle list questions!