Elixir generate inverted / reversed map
defmodule Test do
  def contains?(term, elem) do
    elem in term
  end

  def test do
    elems = [1,2,3]
    terms = [[1,2], [2,3]]
    
    for e &lt;- elems, reduce: %{} do
      acc -&gt;
        key = Enum.find(terms, &amp;contains?(&amp;1, e))
        Map.update(acc, key, [e], &amp;[e|&amp;1])
    end  
    #=&gt; %{[1, 2] =&gt; [2, 1], [2, 3] =&gt; [3]}
  end
end

Found a simple and elegant solution using a reduce comprehension.
Here, we iterate over each elem in elems, find the first term which contains it, then update our map with either term =&gt; [elem] or term =&gt; [elem|previous_elems] depending on if that key exists in the map yet.

Side note:
If I needed the elems in the same as order they were originally I could use &amp; &amp;1 ++ [e] instead of &amp;[e|&amp;1], but as I don't have that requirement it is more efficient to prepend the list.