Python · Collections & Basics

append vs extend

Easylistappendextendmutable

Interview question

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

How to answer

The short answer

Both grow a list in place, but they treat the argument differently.

append(x)

  • Adds x as a single element at the end.
  • Length increases by 1.

extend(iterable)

  • Iterates over iterable and adds each item.
  • Length increases by the number of items yielded.
Python
a = [1, 2]
a.append([3, 4])
print(a)          # [1, 2, [3, 4]]  ← nested list

b = [1, 2]
b.extend([3, 4])
print(b)          # [1, 2, 3, 4]    ← flattened addition

c = [1, 2]
c.extend("ab")
print(c)          # [1, 2, 'a', 'b']  strings are iterable

# + creates a new list; append/extend mutate
d = [1, 2] + [3, 4]   # new object

Interview tip: If someone says "append multiple values," clarify whether they mean one nested object (append) or many elements (extend / +=).

Lock it in with a real drill

You remember an answer longer once you have used it. Try a hands-on problem on this concept.

Open related drill