0%

Lists · practice

Add Items to a List

Replacing an item keeps the same length. To make a list grow, call the list’s .append() .

Add one item to the end

.append() adds exactly one item at the end:

Try it

The existing list changes in place. After the call, packing refers to the three-item list. Keep the call on its own: .append() changes the list rather than returning a new list to . Do not write packing = packing.append("toothbrush"), because that would replace the name’s reference to the list with the method’s .

Call .append() again when another item arrives:

Try it

The calls run from top to bottom, so the appended items keep that order. The list grows one item at a time. Pleasantly literal, for once.

Which line adds "notebook" to the end of a list named supplies?

Watch the list grow

In the exercise, append two supplied items. Check their final order: the order of your calls becomes the order at the end of the list.

Task

Grow the existing packing in two steps:

  1. Append "toothbrush" to the end.

  2. Append "charger" to the end.

  3. Print the completed list.

The final order must be passport, shirt, toothbrush, charger.

If the items appear in the wrong order, press Step in the editor toolbar and watch the list grow after each call.