Marco C. Stewart
Marco C. Stewart

Reputation: 185

How to transform this list into a map?

I have a list of tuples:

[
  {'key1', %{ 'a' => 9543243,
              'b' => 677,
              'c' => 733}},
  {'key2', %{ 'a' => 543,
              'b' => 778,
              'c' => 222}},
  {'key3', %{ ..... }}
]

How to transform it into a map?

%{
  'key1' => %{ 'a' => 9543243,
               'b' => 677,
               'c' => 733 },
  'key2' => %{ 'a' => 543,
               'b' => 778,
               'c' => 222 },
  'key3' => %{ ..... }
}

If I do this via Enum.map/2, it’ll create a new list yet again.

Upvotes: 1

Views: 85

Answers (1)

Matvey Karpov
Matvey Karpov

Reputation: 166

Consider using Enum.into/2

iex(1)> [{"key1", %{"a" => "b"}}, {"key_2", %{"c" => "d"}}] |> Enum.into(%{})
%{"key1" => %{"a" => "b"}, "key_2" => %{"c" => "d"}}

Upvotes: 2

Related Questions