1

I have a list of values that looks something like this:

["Some", "random", "values", [], "in", "a", [], "list"]. 

I would like to create a new list with the empty list items removed. Like this:

["Some", "random", "values", "in", "a", "list"]. 

What is the easiest way of going about this? I assume using list comprehensions to build a new list is going to be the most efficient way of doing this. How would I filter this list using list comprehensions?

2 Answers 2

10

List comprehensions are a neater lists:filter/2:

[E || E <- List, E /= []] 
Sign up to request clarification or add additional context in comments.

Comments

4

This can be achieved using lists:filter.

List = ["Some", "random", "values", [], "in", "a", [], "list"], lists:filter(fun(X) -> X /= [] end, List). 

lists:filter takes a fun and a list. The fun should take a list item and return true or false. If the fun returns true the item is returned in the new list.

More information can be found here: http://erlangcentral.org/wiki/index.php?title=Filtering_Lists

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.