Given this list:
list = [1,2,3,4,5]
how can I return a new list that has these elements?
[3,4,5]
I know I use List.delete/2
but as for as I can tell that will only delete 1 element at a time. I want to remove the first two elements in one fell swoop.
Remove Multiple elements from list by index range using del. Suppose we want to remove multiple elements from a list by index range, then we can use del keyword i.e. It will delete the elements in list from index1 to index2 – 1.
For removing multiple elements from a list by index range we can use del operator.
The List provides removeAll() method to remove all elements of a list that are part of the collection provided.
I think you are looking for this
# Example with static values
iex> [1, 2, 3, 4, 5] -- [1, 2]
[3, 4, 5]
# Example with Enum.drop/2, with multiple values
iex> Enum.drop([1, 2, 3, 4, 5], 2)
[3, 4, 5]
iex> Enum.drop([1, 2, 3, 4, 5], -2)
[1, 2, 3]
iex> Enum.drop([1, 2, 3, 4, 5], 20)
[]
iex> Enum.drop([1, 2, 3, 4, 5], -20)
[]
You can use List.delete/2
only can delete one at time and Enum.drop/2
can delete multiple depend how you use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With