Removing Items from list

Hello all,

I’m playing with nhs API, and after fetching the data and doing some clean up I’m a bit stuck on how to remove items from the list.

I’m doing everything from the iex shell so the code goes something like this

nhs_endpoint = ""
headers = [{"subscription-key", "key"}]

%{status_code: 200, body: body, request_url: request_url} = HTTPoison.get!(nhs_endpoint, headers)
%{"url" => url, "author" => author, "dateModified" => date_created, "description" => title, "mainEntityOfPage" => page, "relatedLink" => link} = Poison.decode!(body)

# Go trough the map of map and map URL into list
l = Enum.flat_map(link, fn %{"relatedLink" => relatedLink} -> relatedLink end)
Enum.flat_map(l, fn %{"relatedLink" => relatedLink} -> relatedLink |> %{"url" => url} -> url end) end)

It will basically boil don’t to this list of URLs


this is a shorter version of the result, but as you see I have an imposter starting with* which I want to remove, so what is a proper way of removing these items?

try Enum.reject/2 with the correct predicate.

Hey @smita sure, I was trying something like this

m |> Enum.reject(fn %{"url" => url} -> url === "*" end) |> %{"url" => url} -> url end)

and m is a flat map of

    "@type" => "LinkRole",
    "linkRelationship" => "Navigation",
    "name" => "Where to give birth: your options",
    "position" => 0,
    "url" => ""
    "@type" => "LinkRole",
    "linkRelationship" => "Navigation",
    "name" => "Antenatal classes",
    "position" => 1,

but that did not work, I assume I’m not using the correct predicate.

Try this:

Enum.reject(arr, &String.starts_with?(&1, ""))

which is the same as

Enum.reject(arr, fn url ->
  String.starts_with?(url, "")

The === operator make strict match, there is no wildcard here. You should use String.starts_with?/2 with second parameter


@stefanluptak thanks, I was reading about String.starts_with? but somehow misunderstood it, thanks for the example, and clarification.

1 Like

Have you considered using Enum.filter/2 and pattern matching?

filtered_list =
   |> Enum.filter(&filter_fun/1)

def filter_fun("" <> _rest) do

def filter_fun(_valid) do