Delete entries from a keyword list

I have a list which contain multiple key value pairs like this;

 [
  id: :id,
  description: :string,
  inserted_by: :integer,
  updated_by: :integer,
  inserted_at: :utc_datetime,
  updated_at: :utc_datetime,
  type_id: :id
  deleted_by: :integer,
 ]

How can i delete the specific tuple from the list like type_id and inserted_by.

for now Enum.reject is working for single tuple:

   Enum.reject(list, fn(k) -> k == {:type_id, :id} end)

Thanks

That is a keyword list. You may want to check the Keyword module functions delete/2, delete/3, delete_first/2, and drop/2. I think Keyword.drop/2 is closest to your description.

Here are the docs: https://hexdocs.pm/elixir/Keyword.html

4 Likes

Try this:

list = [
  id: :id,
  description: :string,
  inserted_by: :integer,
  updated_by: :integer,
  inserted_at: :utc_datetime,
  updated_at: :utc_datetime,
  type_id: :id,
  deleted_by: :integer,
 ]

Keyword.delete(list, :type_id)
2 Likes