Python list.append
Append one item after the current last item of a mutable list.
A simple example
list.append
- Start with List containing A
- Do this Append B
- Result A, B
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.pyAdd 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.
items = ["tea", "rice"]
items.append("oats")
print(", ".join(items))
tea, rice, oats
The original two items keep their order. Oats becomes the third item.
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.
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.
What to watch for
append adds one object. To add every element from another collection, use extend.