Q.Consider the following list myList. What will be the elements of myList after the following two operations:
myList = [10,20,30,40]
i. myList.append([50,60])
ii. myList.extend([80,90])
myList = [10,20,30,40]
append(x) adds x as a single element — so appending a list nests it — while extend(seq) adds each element of seq individually; performing both in sequence turns [10,20,30,40] into [10, 20, 30, 40, [50, 60], 80, 90].
The idea. Both methods grow a list at the end, but they treat their argument differently: append() treats it as one object (whatever its type), extend() iterates over it. This is the single most-tested difference between list methods.
Working through the two operations in order:
myList = [10,20,30,40]
myList.append([50,60])
print(myList)
myList.extend([80,90])
print(myList)
| Operation | What happens | myList afterwards | Length |
|---|---|---|---|
myList.append([50,60]) | the list [50, 60] goes in as ONE element (nested) | [10, 20, 30, 40, [50, 60]] | 5 |
myList.extend([80,90]) | 80 and 90 are appended one by one | [10, 20, 30, 40, [50, 60], 80, 90] | 7 |
Expected output:
[10, 20, 30, 40, [50, 60]]
[10, 20, 30, 40, [50, 60], 80, 90]
Note the nested [50, 60] is a single element: myList[4] is the list [50, 60], and myList[4][1] is 60.
(If the two operations are instead each applied to the original [10,20,30,40] independently, the results are [10, 20, 30, 40, [50, 60]] for i and [10, 20, 30, 40, 80, 90] for ii — the append/extend behaviour is the same either way.)
After the append, len(myList) is 5, not 6 — a nested list counts as one element no matter how many items it contains. Confusing that is the standard mark-loser on this question.
i gives [10, 20, 30, 40, [50, 60]] (append nests the whole list as one element); ii then gives [10, 20, 30, 40, [50, 60], 80, 90] (extend adds 80 and 90 individually).
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.