Lists, arrays & nested collections
Instead of creating individual variables for every single item (`item1`, `item2`, `item3`), a List bundles ordered elements into a single data structure that can grow and shrink dynamically.
- Create, index, and modify lists.
- Add and remove items with .append() and .pop().
- Work with nested 2D lists representing a game matrix.
List methods, indexing, appending, and slicing.
Key methods: .append(x), .insert(i, x), .pop(), .remove(x), and len(). Negative indexing allows easy access to the end with list[-1].
List operations: Indexing, Appending, Popping
Lists use square brackets `[]`. Elements can be accessed by index, modified in place, or appended to the end.
2D Lists: Treasure Map Matrix
Lists can contain other lists! A 2D list creates rows and columns, just like a game board or our Code Arena grid.
Memory Trace: 2D Matrix Grid Indexing
Observe how nested arrays update specific row and column coordinates in memory.
| Variable | Value in Memory | Data Type |
|---|---|---|
| grid[0] | ["-", "-"] | auto |
| grid[1] | ["-", "-"] | auto |
Two rows created. grid[0] is Row 0, grid[1] is Row 1.
grid = [["-", "-"], ["-", "-"]]🔧 List Shallow Reference Bug
A student tried to copy a list with b = a, but modifying b mutated a because both pointed to the same memory.
Predict The Output (2 Questions)
Your lightbulb moment
The key takeaway
Lists store ordered sequences that can grow dynamically. Nested lists allow building 2D grids for games and tables.
Make it yours
Try adding a fourth item using .append() and access it with negative indexing fruits[-1].
Try editing the code in the playground above in Python, JavaScript, Java, or C++, and use the step-by-step Dry-Run Table to inspect how variables mutate in memory. If you get stuck, click the 🤖 AI Doubt Solver button on your screen for instant explanations!