Exercises · Q5
Q.Differentiate between append() and extend() methods of list.
Odisha ChseTextbookSubjective· 3mImportance★★★★★est
43% · 13/30 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 its argument as one single element (even if it's a list — it gets nested); extend(iterable) iterates over its argument and adds each element separately. After adding a 2-element list, append grows the list by 1, extend by 2.
Side-by-side demonstration:
list1 = [10, 20, 30]
list2 = [10, 20, 30]
list1.append([40, 50]) # one nested element in
list2.extend([40, 50]) # two separate elements in
print("append:", list1, "-> length", len(list1))
print("extend:", list2, "-> length", len(list2))
append: [10, 20, 30, [40, 50]] -> length 4
extend: [10, 20, 30, 40, 50] -> length 5
The differences, point by point
| Aspect | append(x) | extend(iterable) |
|---|---|---|
| What it adds | x as one element, whatever it is | every element of the iterable, one by one |
| Length grows by | exactly 1 | len(iterable) |
| Passing a list | the list is nested inside | its elements are merged in flat |
| Argument type | anything (int, string, list, …) | must be an iterable (list, string, tuple, …) |
| In place? | yes, returns None | yes, returns None |
Why the difference matters
list1.append([40, 50])makeslist1[3]the list[40, 50]— to reach 40 you'd writelist1[3][0]. Usually that nesting is an accident, caused by usingappendwhereextendwas meant. …
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.