Elixir remove nil from list

17,997

Enum.filter/2 comes to the rescue:

Enum.filter(list, & !is_nil(&1))

If you are certain there could not be false atoms in the list (the other possible falsey value in Elixir,) the filter might be simplified to:

Enum.filter(list, & &1)

Also (credits to @Dogbert) there is a counterpart function Enum.reject/2 that “returns elements of enumerable for which the function fun returns false or nil.”

Enum.reject(list, &is_nil/1)
Share:
17,997

Related videos on Youtube

Sardoan
Author by

Sardoan

Started with my 386 SX with dos and qbasic. Was 8 yeas old and droped qbasic and switched to dr.dos. Growing up I played games with Win 95, 98 and 2000. With Win 2000 I started to digging deeper. With XP in 2002 I started to make some HTML-stuff. Had my first ANSI-C course at the university and got in touch with programming, not the stuff I tried with qbasic. Used Unix and Linux for a while, but these OS are no end-user OS. So I am no Linux-fan. Linux is not for desktop, it is for Servers. Only 1% of the user think it is for the desktop. Keep fighting dudes, ;) Maybe you will reach the 2%. OK, 2007 i liked KNOPPIX. Backtrack and RUSSIX are cool too. But only for special use. Computers became my best friends again ;) and I made a lot of funny things with them. This time noone had a firewall and the admin-password was empty :) After learning pearl, vb.net, c#, c++, java se/ee, php, javascript, typescript, less etc I am now certified in ITIL, ISTQB-softwaretester, Microsoft Specialist for HTML5, Javascript and CSS3 and Microsoft Certified Solution Developer For Web Applications. Now working with asp.net mvc, knockout.js, jquery, WCF and all the stuff, that is needed for REST-apps. Switched about 1.5 years ago from Java wit JSF to .NET. Was one of the 90% who hates JSF, wicket rules ;) and .NET is even better :). Working now for a company in different projects. Now for webappications in automotive, traffic and other stuff.

Updated on September 25, 2022

Comments

  • Sardoan
    Sardoan over 1 year

    I got the following structure:

    [nil,
     %{attributes: %{updated_at: ~N[2017-09-21 08:34:11.899360]},
     ...]
    

    I want to remove the nils. How do I do that? Tried Enum.reduce/3 but it didn't work.

  • Sardoan
    Sardoan over 6 years
    Ahhh, nice. Tried something like List.delete. Filter, thanks. Solution was data = Enum.filter(data, & !is_nil(&1))
  • Sardoan
    Sardoan over 6 years
    False is not included, so I need only to check for nil. Your solution works perfect. Thanks again :) I make a query before for the last changes in the databse. With a new databse I got some nils and had to remove them.
  • Dogbert
    Dogbert over 6 years
    Also: Enum.reject(list, &is_nil/1).
  • sambecker
    sambecker over 5 years
    Enum.reject(list, &is_nil/1) is particularly elegant—nice solution
  • Julia
    Julia about 3 years
    Hi! Please edit your answer to expand a little bit upon how the code snippet you posted works. Thanks!