EasyπŸ‘€ 0-2 years 1 min read

What is the difference between append() and extend() on a list?

Asked inTCSInfosysCognizant
#list#append#extend#python basics#collections
Report issue

⚑ 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

append(x)β†’adds ONE element (x itself)
extend(it)β†’adds each element of it
a + b→returns a NEW list (no mutation)

⌨️Hands-on Keyboard

Learn by Doing

python
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]
Output
[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:

βœ“ append adds one elementβœ“ extend adds each elementβœ“ append([...]) nestsβœ“ += behaves like extend

⚠️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

list methodsiterablesamortized O(1) append

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?

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.

Related Questions