owaisqayum

owaisqayum

Filtering list of lists

I am having a list of lists and i want to obtain an output by filtering them.

list =
[
  [[key: 1, value: 2], [key: 1, value: 2]],
  [[key: 1, value: 2], [key: 2, value: 3]],
  [[key: 2, value: 3], [key: 1, value: 2]],
  [[key: 2, value: 3], [key: 2, value: 3]]
]

here In each list i want to filter out those values where the keys are equal.

For example, in first list we have

[[key: 1, value: 2], [key: 1, value: 2]]

here both keys are equal, so how can we filter only those results where both the keys are equal.

Thanks

Marked As Solved

owaisqayum

owaisqayum

So i have worked on it and found the solution for it

defmodule J do
  def join(list_of_lists) do
    join_single_row(list_of_lists, [])
    |> Enum.map(fn row -> List.flatten(row) end)
  end

  def join_single_row([], final), do: final

  def join_single_row([row | rest_of_rows], final) do
    length =
      Enum.map(row, fn [key, _value] -> key end)
      |> Enum.uniq()
      |> length()

    case length do
      1 ->
        join_single_row(rest_of_rows, final ++ [row])

      _ ->
        join_single_row(rest_of_rows, final)
    end
  end
end

Also Liked

gregvaughn

gregvaughn

:golf: :grin:

iex(5)> list =
...(5)> [
...(5)>   [[key: 1, value: 2], [key: 1, value: 2]],
...(5)>   [[key: 1, value: 2], [key: 2, value: 3]],
...(5)>   [[key: 2, value: 3], [key: 1, value: 2]],
...(5)>   [[key: 2, value: 3], [key: 2, value: 3]]
...(5)> ]

iex(6)> for [left, right] = row <- list, left[:key] == right[:key], do: row
[
  [[key: 1, value: 2], [key: 1, value: 2]],
  [[key: 2, value: 3], [key: 2, value: 3]]
]
kokolegorille

kokolegorille

Using Enum.map with Enum.filter would have worked too…

Please note that [key: :value] is a Keyword list… a special kind of list.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement