Python Fundamentals · Built-in Data Structures
20 cards
Lists, Stacks, and Queues
-
Quick check
`plan` holds one task and `extra` holds two more tasks. Which call leaves `plan` holding the three tasks as separate items?
A`plan.append(extra)`, which adds the pair as one item
`append()` adds its argument as a single item, so the pair would sit inside the list as one nested element.
B`plan.extend(extra)`, which adds each of its items
Right. `extend()` consumes the iterable and appends every item it contains, giving three separate tasks.
C`plan.insert(0, extra)`, which puts the pair at the front
`insert()` also stores the argument as one item, and it places that item before the current first element.
3 / 20
-
Quick check
`values.remove(target)` runs on a list where `target` appears three times. What happens?
AThe first equal item is deleted and the later matches stay
Right. One call deletes the first equal item, so the remaining matches are untouched.
BEvery equal item is deleted and their former count is returned
One call removes one item, and it does not report how many equal items existed.
CThe position of the first match is reported instead
Reporting a position is the job of `index()`; `remove()` changes the list instead.
6 / 20
-
Quick check
A program runs `result = names.sort()`. What is true afterwards?
A`result` holds a sorted copy and `names` keeps its original order
The method does not build a copy; it reorders the list it was called on.
B`result` holds the removed last item and `names` is now shorter
Removing an item and handing it back is what `pop()` does, not `sort()`.
C`names` is sorted in place and `result` is `None`
Right. `sort()` orders the list in place, and an in-place list method returns `None`.
9 / 20
-
Keep your progress in the app
That’s 3 of 7 quick checks. In the app they stay answered, and every lesson remembers where you left off.
-
Quick check
An undo history adds each new action at one end and must retrieve the most recent action first. Which pair does that?
AAdd with `append()` and retrieve with index-free `pop()`
Right. Both operations stay at the end of the list, so the newest action is the one that comes back.
BAdd with `append()` and retrieve with `pop(0)`
Popping index zero takes the oldest action under this insertion pattern, which is queue order.
CAdd with `insert(0, action)` and retrieve with index-free `pop()`
Inserting at the front and popping at the back also returns an older action before the newest one.
12 / 20
-
Quick check
A service must handle arrivals in their original order, adding and removing constantly without shifting the remaining elements. Which design fits?
AA list, adding with `insert(0, item)` and removing with `pop()`
The order is right, but front insertion in a list is slow because every remaining element shifts.
BA `collections.deque`, adding with `append()` and removing with `popleft()`
Right. A deque is fast at both ends, and `popleft()` returns the earliest arrival.
CA `collections.deque`, adding and removing with index-free `pop()`
Adding and popping at the same end returns the newest arrival first, which is stack behavior.
15 / 20
-
Quick check
Positions 2 to 4 of `records` must go, the removed values are not needed, and the name must stay usable. Which statement fits?
A`records.pop(2:5)`, ignoring the item it hands back and moving on
`pop()` takes one position, not a slice, and it hands the removed item back.
B`records.remove(2:5)`, then keep using the list
`remove()` searches for a value equal to its argument and does not read a slice as positions.
C`del records[2:5]`, which leaves the name in place
Right. A slice target deletes those positions, returns nothing, and keeps the name bound.
18 / 20
-
Quick check
Which line correctly separates the three jobs of adding, retrieving and deleting?
A`extend()` adds one item, `pop(0)` is fast on a long list, and `del` returns the value it removed
`extend()` adds every item of an iterable, `pop(0)` shifts the rest of the list, and `del` produces no value.
B`append()` adds one item, index-free `pop()` returns the last one, and `del values[0]` removes without returning
Right. Those are the documented roles of `append()`, index-free `pop()` and an indexed `del`.
C`insert()` returns the changed list, `popleft()` works on any list, and `remove()` deletes by position
In-place methods return `None`, `popleft()` belongs to a deque, and `remove()` selects by value.
20 / 20
-
7 quick checks · then the test
In the app, finishing the quick checks opens this lesson’s 10-question test, and the ones you miss come back exactly when you’re about to forget them.
The whole course, on your phone
Lessons you can read, audio you can listen to on the way to work, and practice that remembers what you got wrong.