πŸ–₯️
Berkeley CS61A: Notes
  • 🌍Hello, World!
  • πŸ’­Learn to Think Like Your Computer
  • Building Blocks
    • ❗Expressions in Python
      • ℹ️Mathematical Statements
      • 🀫Variables
      • πŸ‘Data Types in Python
    • 🚧Introduction to Functions
      • πŸƒβ€β™€οΈHow Python Executes a Function Call
      • 🚱Functions that Don't Return Anything
    • 🌊Control Flow
      • ⁉️What is an if statement?
      • πŸ’ΎWhat is a Loop?
      • 🚨What are Logical Operators?
        • πŸ˜–Logical Operators, Seemingly Illogical Behavior
      • 🚰How Does Control Flow?
    • ⚑Higher-Order Functions
      • ⏸️What are Higher-Order Functions Used For?
      • πŸ‘†Self-Reference
    • πŸ‘ΎEnvironments and Scope
      • πŸ–ŠοΈHow to Make Environment Diagrams
  • STRUCTURES OF DATA
    • ➰Recursion
      • πŸ«€Anatomy of Recursion
      • β›½Recursive Leap of Faith
    • πŸŽ„Tree Recursion
      • ⬇️The Use It/Lose It Principle
    • πŸ“€Iterables
      • βœ‚οΈList Slicing
      • πŸ’€Deep Lists
      • πŸŒ‹Iterable Functions
      • πŸ’½List Comprehensions
    • πŸ™ˆAbstraction
    • Trees
    • Linked Lists
    • Iterators and Generators
    • Efficiency
  • Extra Topics
    • 🎭Errors and their Types
      • 🏹Error vs Exception
      • πŸ”™What is Backtracing?
    • 🚑How the Terminal and the File are Different
    • 🍬Decorators
  • Debugging Tools
    • πŸ–¨οΈDebugging with Print Statements
    • πŸ›Debugging with the Debugger
Powered by GitBook
On this page

Was this helpful?

  1. STRUCTURES OF DATA
  2. Iterables

Deep Lists

Visualizing lists.

PreviousList SlicingNextIterable Functions

Last updated 3 years ago

Was this helpful?

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:

lst = [1, [2, 3], [4, 5, 6]]

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 :

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!

πŸ“€
πŸ’€
PythonTutor
List Visualized