Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete multiple elements from a list

Tags:

elixir

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.

like image 419
Bitwise Avatar asked Nov 01 '18 01:11

Bitwise


People also ask

How do you remove multiple elements from a list in Python?

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.

Which function is used to delete multiple elements of a list?

For removing multiple elements from a list by index range we can use del operator.

How do I remove multiple elements from a list in Java?

The List provides removeAll() method to remove all elements of a list that are part of the collection provided.


Video Answer


1 Answers

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.

like image 119
DarckBlezzer Avatar answered Sep 18 '22 15:09

DarckBlezzer