Unit 8: Arrays, Lists, and Strings
Contiguous storage, zero-based indexing, and the sequence type every other data structure gets compared against.
7 topics · 22 questions
Each topic is a path, not a menu: work down it in order and every question builds on the one above. Click any of them to watch the answer draw itself.
Arrays and Zero-Based Indexing
- How it worksHow does an array lay its elements out back to back so any index can be reached in one calculation?
- Worked exampleTrace a = [10, 20, 30, 40] and find a[0], a[2], and a[3].
- Common mistakeWhy isn't the last legal index of an array the same number as its length?
Your turn1 of 2
An array holds 697 elements, indexed starting at 0. What is the index of its last element?List Operations and Their Cost
- How it worksHow does inserting at the front of a list force every other element to shift over by one spot?
- Worked exampleTrace lst = [1, 2, 3] after append(4), then insert(0, 0), then remove(2).
- Going furtherWhat happens to the cost of an insert once the list holds a million elements instead of ten?
Your turn1 of 2
Inserting at the front of a list forces every one of its 696417 existing elements to shift over by one spot. How many elements need to shift for this single front insertion?Two-Dimensional Arrays and Grids
- How it worksHow does a grid of rows and columns sit inside a list of lists, one row per outer element?
- Worked exampleTrace grid[1][2] for grid = [[1,2,3],[4,5,6],[7,8,9]].
- Common mistakeWhy isn't grid[1][2] the same cell as grid[2][1]?
Your turn1 of 2
A grid has 35 rows and 33 columns, stored as a list of lists. How many total cells does the grid contain?Strings as Sequences of Characters
- How it worksHow does slicing a string with a start and stop index pull out a substring without touching the original?
- Worked exampleTrace word = 'python' and find word[0], word[-1], and word[1:4].
- Going furtherWhat happens to a slice's result when the stop index reaches past the end of the string?
Your turn1 of 2
A string has 349 characters, indexed starting at 0. What is the index of its last character?String Immutability
- How it worksHow does every string method that looks like it edits a string actually build and return a brand new one?
- Worked exampleTrace s = 'hello' then t = s.upper() and find whether s itself changed.
- Common mistakeWhy doesn't calling a method like replace on a string change the original string in place?
- Ties togetherHow does string immutability connect to the same reason a tuple can't be changed after creation?
Your turn1 of 2
A string s of 35 characters has a method called on it that returns a brand new string of 33 characters, leaving s unchanged. How many total characters exist between s and the new string, added together?Efficient String Building
- How it worksHow does each += on a string inside a loop copy the entire string built so far into a brand new one?
- Worked exampleTrace the total characters copied when building a 5-character string one letter at a time with +=.
- Going furtherWhat changes in performance once the loop collects pieces in a list and joins them only once at the end?
Your turn1 of 2
A loop builds a string one character at a time using +=, and each += copies the entire string built so far. For a final string of 350 characters, roughly how many total characters get copied across the whole loop?Out-of-Bounds Access and Off-by-One on Arrays
- How it worksHow does an index equal to or past an array's length reach past the memory the array actually owns?
- Worked exampleTrace a loop from i=0 to i=len(a) inclusive over a=[1,2,3] and find exactly where it fails.
- Common mistakeWhy doesn't an array automatically grow just because you tried to index one past its end?
Your turn1 of 2
An array a has 697 elements, with valid indices 0 through n minus 1. A loop runs from i=0 through i=697 inclusive, one too many passes. At which index i does the loop first fail?