Library contents

Python list.append

Append one item after the current last item of a mutable list.

A simple example

list.append

  1. Start with List containing A
  2. Do this Append B
  3. Result A, B
Appending puts the new element after the last existing element.

Syntax

Python syntax
items.append(item)
Run these examples in Python

Save one example at a time as lesson.py. Use Python 3.9 or later.

python3 lesson.py

Add a late item to a shopping list

A shopper adds an item after starting a list. The program must preserve what is already in the list and make the late addition visible when the list is displayed.

From the programitems = ["tea", "rice"]
Resulttea, rice, oats
lesson.py
items = ["tea", "rice"]
items.append("oats")
print(", ".join(items))
Output
tea, rice, oats

The original two items keep their order. Oats becomes the third item.

Advertisements

Extend a reading list

A class has chosen one novel and adds two more titles. The desk adds the new entries after the existing ones. Read the resulting sequence in its recorded order.

GivenFern + Tide, Sky
Worked resultFern · Tide · Sky

Update an equipment checklist

A technician adds the final item after packing two pieces of equipment. The desk adds the new entries after the existing ones. Read the resulting sequence in its recorded order.

GivenLens, Stand + Cable
Worked resultLens · Stand · Cable

What to watch for

append adds one object. To add every element from another collection, use extend.

Documentation for list.append