Concept
Removing Items: `.pop()`, `.remove()`, `del`
`.remove(val)` deletes by value. `.pop(index)` removes and hands you the item (defaults to the last one). `del list[index]` deletes directly.
python
nums = [10, 20, 30, 40]
nums.remove(20) # [10, 30, 40]
last = nums.pop() # removes 40
print(nums) # [10, 30]
# 70. Removing Items: `.pop()`, `.remove()`, `del` `.remove(val)` deletes by value. `.pop(index)` removes and hands you the item (defaults to the last one). `del list[index]` deletes directly: ```python nums = [10, 20, 30, 40] nums.remove(20) # [10, 30, 40] last = nums.pop() # removes 40 print(nums) # [10, 30] ``` Warning: `.remove()` searches from left to right and deletes ONLY the very first match. If the item doesn't exist, it explodes with a `ValueError`.
Checkpoint
Which method removes the last element and returns it to you?
Practice
Remove the word `"trash"` from `bag = ["gems", "trash"]` using `.remove()`, then print `bag`.
01
Tap code blocks below to assemble
Available Blocks