0%

Lists · practice

Remove Items from a List

can shrink as well as grow. The right removal depends on what you know: the item’s or its position.

Remove a known value

.remove(value) deletes the first matching value:

Try it

If the value appears more than once, only its first occurrence is removed. .remove() changes the list; it does not return the removed item. Use the call on its own, as shown above. If the value is absent, Python raises .

Remove and keep an item

.pop(index) removes an item and returns it:

Try it

Here, "manual" is removed from 1 and stored in finished. Calling pop() without an index removes and returns the last item. An index that does not exist, including any index in an empty list, raises IndexError.

Choose the method that matches the job:

  • remove() uses a value.

  • pop() uses a position and returns the removed item.

Which operation removes an item by index and gives the removed value back?

The exercise combines remove() and pop() in a predictable sequence. Work from top to bottom, because the first removal changes the indexes used by the second.

Task

Starting with the provided reading :

  1. Remove "magazine" by .

  2. Pop the item at 1 and store it in finished.

  3. Print finished, then print the remaining list.

The finished item should be "manual". The remaining list should contain "novel" and "poetry".