A programmer writes: words = ['banana', 'apple', 'cherry']; words = words.sort(); print(words). What does this print?
A['apple', 'banana', 'cherry'] — sort() returns the sorted list
BNone — sort() modifies the list in place and returns None, so words is now None
C['banana', 'apple', 'cherry'] — sort() on strings is not supported
DAn error — you cannot sort a list and reassign it in one line
sort() is an in-place method that modifies the list directly and returns None. When you write `words = words.sort()`, you call sort() (which successfully reorders the list in memory), then assign its return value — None — back to words. The sorted list is lost because you've overwritten the variable with None. The fix is either `words.sort()` (don't capture the return value) or `words = sorted(words)` (use the built-in function that returns a new sorted list). This is the single most common list operations bug.
Question 2 Multiple Choice
You have the list [3, 1, 4, 1, 5] and call remove(1). What is the resulting list?
A[3, 4, 5] — remove() deletes all occurrences of the value
B[3, 4, 1, 5] — remove() deletes the first occurrence of the value
C[3, 1, 4, 5] — remove() deletes the last occurrence of the value
D[1, 4, 1, 5] — remove() deletes the element at index 0 (the first 3)
remove(value) searches linearly from left to right and deletes only the first occurrence of the given value. With [3, 1, 4, 1, 5], the first occurrence of 1 is at index 1, so the result is [3, 4, 1, 5] — the second 1 (at index 3) is untouched. If you need to remove all occurrences, you'd need a loop or a list comprehension. Option D confuses remove() (finds by value) with pop() (finds by index).
Question 3 True / False
sorted(my_list) modifies my_list in place and returns the sorted version of the same list.
TTrue
FFalse
Answer: False
sorted() is a built-in function that takes any iterable and returns a brand-new sorted list, leaving the original completely unchanged. This is the key distinction from sort(): my_list.sort() modifies in place and returns None; sorted(my_list) does not modify my_list and returns a new sorted list. Use sorted() when you need to preserve the original order while also having a sorted version.
Question 4 True / False
pop() called with no argument removes and returns the last element of the list.
TTrue
FFalse
Answer: True
pop() with no argument defaults to removing the element at index -1 (the last element) and returns it. This is the standard way to use a list as a stack (LIFO: last-in, first-out). You can also call pop(0) to remove and return the first element (though this is O(n) and a deque is more efficient for that use case). The return value is what distinguishes pop from remove: pop gives you the removed element back; remove just discards it.
Question 5 Short Answer
What is the difference between remove() and pop(), and when would you choose one over the other?
Think about your answer, then reveal below.
Model answer: remove(value) finds and deletes the first element that matches a given value; it raises a ValueError if the value isn't found. pop(index) removes and returns the element at a specific position; with no argument it removes the last element. Choose remove() when you know the value you want to delete but not where it is. Choose pop() when you need the removed element back, when you're treating the list as a stack (pop() for last element), or when you know the position of what you want to remove.
The confusion between them is compounded by lists of integers, where a value might coincidentally equal a valid index. For example, in [3, 1, 4], calling remove(1) deletes the value 1, while pop(1) removes whatever is at index 1 (the value 1 in this case, coincidentally). In a different list like [3, 5, 4], remove(1) would raise ValueError while pop(1) would remove 5. Understanding this distinction prevents subtle bugs when working with lists of numbers.