How to create a map from a list of two item tuples in Elixir

What would be an elegant way for converting a list of two item tuples like [{1,2},{3,4}] into the map %{1=>2, 3=>4}?

Keyword list would be trivial, but what if we have arbitrary keys?


Solution 1:

The simplest way to do this is:

Enum.into(list, %{})

Solution 2:

Map module also supports such lists as a parameter to the new function:

iex> Map.new([{1, 2}, {3, 4}])
%{1 => 2, 3 => 4}

Solution 3:

I've just got it:

list = [{1,2},{3,4}]
themap = for e <- list, into: %{}, do: e