Lists · practice
Remove Items from a List
Remove a known value
.remove(value) deletes the first matching value:
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:
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
Remove
"magazine"by. Pop the item at
1and store it infinished.Print
finished, then print the remaining list.
The finished item should be "manual". The remaining list should contain "novel" and "poetry".