Last time, we learned how to add items to the end of a list using the .append()
method. This lets our lists grow.
But what if we need to take an item out of a list?
Engagement Message
For example, removing a completed task. How might we do that?
Python lists have another helpful method called .remove()
. This method searches the list for a specific value and removes the first item that matches it.
It modifies the list directly.
Engagement Message
Does .remove()
seem useful for managing a shopping list?
The syntax is list_name.remove(value_to_remove)
.
If we have shopping = ["milk", "eggs", "bread"]
and run shopping.remove("eggs")
, the list changes.
Engagement Message
What do you think the shopping
list contains after removing "eggs"?
After shopping.remove("eggs")
, the list becomes ["milk", "bread"]
. The item "eggs"
is gone.
