Technical skill practice

append in Python: Add One Item and Compare extend

In Python, list.append adds one object to the end of a list and changes that list in place. It returns None. If the object is another list, that entire list becomes one item; use extend when you intend to add each item from an iterable.

Start with a small, explicit task

Start a fictional batch queue with one ID. Add one more ID, then receive a batch of two IDs. Compare appending the batch as a single grouped item with extending a separate queue by its contents. Decide whether the nested queue shape is intentional before choosing the method.

Run the complete example

This original example was checked with Python 3.12.14. Save it as python_append.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.

Expected output

None

['B1', 'B2', ['B3', 'B4']]

['B1', 'B2', 'B3', 'B4']

queue = ["B1"]
returned = queue.append("B2")
grouped = queue.copy()
batch = ["B3", "B4"]
grouped.append(batch)
queue.extend(batch)
assert returned is None
assert queue == ["B1", "B2", "B3", "B4"]
assert grouped == ["B1", "B2", ["B3", "B4"]]
assert len(grouped) == 3 and len(queue) == 4
print(returned)
print(grouped)
print(queue)

Explain the result

The first append updates queue before its return value is stored in returned. That return is None, while the queue remains a list. The grouped copy receives batch as one object, so its length is three and its final element is a list. The original queue receives two separate strings through extend, giving four outer items.

Both structures can be valid representations: one preserves a batch as a group, and the other flattens this one level into a processing queue. The code checks the shape explicitly because merely printing similar IDs can conceal nesting. No recursive flattening occurs; extend only iterates over the object you supply.

Avoid these mistakes

Do not write queue = queue.append(value): it replaces the variable with None after mutating the old list. Also be careful with strings passed to extend; a string is iterable, so its characters become separate list items. Use append when the complete string should be one queue entry.

Questions, answered

Why does append print None?

You are inspecting its return value. Check the list itself to see the mutation.

Will append flatten another list?

No. It adds that list as one object; the grouped result demonstrates the extra nesting.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.