remove() / pop()
Removes an element (by value or by position).
Syntax
items.remove(value) · items.pop(index)
Description
remove() deletes the given VALUE (the first match). pop() deletes the element AT the given POSITION and returns it; if no index is given, it takes the last one.
Returns
pop() returns the removed element.
Example
Loading editor…
Expected output
['x'] z
When to use?
Taking a product out of a cart, taking an element from a queue.
Common mistakes
- ⚠️ remove() throws an error for a value that doesn't exist; check 'value in items' first.