What is the difference between append() and extend() on a list?
β‘ Short Answer
`append(x)` adds x as a single element (the list grows by one, even if x is itself a list). `extend(iterable)` adds each element of the iterable individually (the list grows by len(iterable)). Use append for one item, extend to merge another sequence in.
βCoffee Chat Question
Concept Made Simple
βWhat is the difference between append() and extend() on a list?β
π§ Mind Map Answer
Remember It Faster
β¨οΈHands-on Keyboard
Learn by Doing
nums = [1, 2]
nums.append([3, 4]) # nested
print(nums) # [1, 2, [3, 4]]
nums = [1, 2]
nums.extend([3, 4]) # flattened in
print(nums) # [1, 2, 3, 4][1, 2, [3, 4]] [1, 2, 3, 4]
π₯What If?
Think Beyond the Expected
What does `nums.append([3, 4])` produce and why does it surprise people?
It produces `[1, 2, [3, 4]]` β the whole list is added as a single nested element, not merged. People expect flattening; that's what `extend` (or `+=`) does. append always adds exactly one item, whatever its type.
πReal World
Accidentally using append where you meant extend creates nested lists that break later iteration β a very common beginner bug when building up results from multiple sources.
π―Interviewer's Expectation
Keywords they're listening for:
β οΈCommon Mistakes
- βUsing append when you meant extend (nesting)
- βAssuming a + b mutates a
- βConfusing extend with a shallow copy
β Best Practices
- βappend for a single item, extend to merge
- βUse += for in-place extend
- βPrefer list comprehension to flatten nested lists
πFollow-up Questions
- 1How is `list += other` different from `list = list + other`?
- 2What's the time complexity of append?
- 3How do you flatten a list of lists?
π§©Related Technologies
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly β AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Lists (Python) Interview question: "What is the difference between append() and extend() on a list?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
β Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission β at no extra cost to you. It helps keep Full Stack Interview Guru free.