List methods: append and remove

Lists can change. append adds to the end, and remove takes out the first matching item.

Syntax

items.append(x)
items.remove(x)
items[0] # first item
  • Positions start at 0. items[-1] is the last item.

Example

parts = ["wrench"]
parts.append("cable")
parts.remove("wrench")
print(parts)

Output:

['cable']

Common mistakes

  • append changes the list and returns None. Writing x = x.append(1) makes x None.
  • remove raises a ValueError if the item is not in the list.

Practise it