How do I remove an item from a list in EK9?
← Common Collection Tasks · Ref: Q181
EK9 provides two removal operators: -= for mutating the list and - for creating a new list without the item.
MUTATING REMOVE (-=)
Remove the first occurrence of an item:
numbers -= 3
Modifies the list in place. If the item is not found, no change.
NEW LIST (-)
Create a new list without the item:
filtered <- numbers - 3
The original list is unchanged.
STREAM FILTER FOR COMPLEX REMOVAL
Remove items matching a condition using stream pipeline:
cat numbers | filter by isPositive | collect as List of Integer
See Q45 for List basics. See Q88 for List operations. See Q89 for stream pipelines.
Example
defines module qa.collectiontasks.listremove defines function isGreaterThanTwo() as pure -> num as Integer <- rtn <- Boolean() expectedSize <- 2 rtn: num > expectedSize defines program ListRemoveDemo() stdout <- Stdout() // === MUTATING REMOVE (-=) === numbers <- [1, 2, 3, 4, 5] stdout.println(`Before: ${numbers}`) numbers -= 3 stdout.println(`After -= 3: ${numbers}`) // Remove item not in list — no change numbers -= 99 stdout.println(`After -= 99 (no change): ${numbers}`) // === NEW LIST (-) === original <- [10, 20, 30, 40] without30 <- original - 30 stdout.println(`Original: ${original}`) stdout.println(`Without 30: ${without30}`) // === STREAM FILTER FOR COMPLEX REMOVAL === // Keep only items greater than 2 mixed <- [1, 2, 3, 4, 5] filtered <- cat mixed | filter by isGreaterThanTwo | collect as List of Integer stdout.println(`Filtered (> 2): ${filtered}`) // === REMOVE FROM STRINGS === names <- ["Alice", "Bob", "Charlie"] names -= "Bob" stdout.println(`After removing Bob: ${names}`)
Common mistakes
E50060 — EK9 uses the -= operator to remove items from a list, not a remove() method. See ek9 -h E50060 for details.
Incorrect:
numbers.remove(3)
Correct:
numbers -= 3
Other ways to ask this
- How do I delete an element from a list in EK9?
- What is the EK9 equivalent of list.remove()?
- How do I drop an item from a list in EK9?
Coming from another language?
Java: list.remove(item), list.removeIf(predicate). Python: list.remove(item), list comprehension for filtered. Rust: vec.retain(predicate), vec.remove(index). Go: manual loop and slice manipulation. JavaScript: arr.filter(), splice for in-place. Kotlin: list.minus(item), mutableList.remove(). EK9: list -= item for mutating, list - item for new list, stream filter for complex removal.
Keywords: collection, remove, mutate, element, minus, delete, task, drop, list, item, filter