Example 1 — Merge Grocery Items Into a Cart
Extend with a list, then see what happens when a single string is passed by mistake.
cart = ["bread", "eggs"]
print(cart.extend(["jam", "butter"]))
print(cart)
cart.extend("tea")
print(cart)
del cart[-3:]
cart.extend(["tea"])
print(cart) How It Works
- Nothing comes back.
extend()returnsNone, while the two new groceries are added tocartitself. - Two separate items. "jam" and "butter" join the cart as two elements, not as one nested list.
- Strings split into letters. A string is iterable one character at a time, so
extend("tea")adds three single letters. - Undo and fix.
del cart[-3:]removes the three letters, and wrapping the word in a list adds "tea" as one item.

