Exercise · Q5
Q.Differentiate between append() and extend() functions of list.
Tamil Nadu DgeTextbookSubjective· 3mImportance★★★★★est
73% · 19/26 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →append(x) adds x to the end as ONE element (so appending a list nests it and the length grows by exactly 1), while extend(seq) walks through seq and appends each of its elements individually (length grows by len(seq)).
The idea. Both are in-place list methods that grow a list at its end and return None — the difference is entirely in how the argument is treated: as an object (append) versus as an iterable of objects (extend).
Point-by-point comparison:
| Aspect | append(x) | extend(seq) |
|---|---|---|
| What is added | x itself, as a single element | each element of seq, one by one |
| Effect of passing a list | the list is nested inside | its items are merged in flat |
| Growth in length | always +1 | +len(seq) |
| Argument requirement | any object at all | must be iterable (list, tuple, string…) |
| Equivalent operation | L[len(L):] = [x] | L = L + list(seq) done in place (like +=) |
Demonstration:
L1 = [10, 20]
L1.append([30, 40]) # one nested element
print(L1, "-> length", len(L1))
L2 = [10, 20]
L2.extend([30, 40]) # two separate elements
print(L2, "-> length", len(L2))
L3 = [10, 20]
L3.extend("ab") # a string is iterable: adds 'a', 'b'
print(L3)
Expected output:
[10, 20, [30, 40]] -> length 3
[10, 20, 30, 40] -> length 4
[10, 20, 'a', 'b']
``` …
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.