Converting elixir list into a list with maps

how do i convert a list into a list with maps

like this list [ "red", "green","black"] into

[ %{ name:"red", sn = 1},  %{ name:"green", sn = 2}, %{ name:"black", sn = 3} 
]

I would use Enum.with_index and Enum.map

list
|> Enum.with_index(1)
|> Enum.map(fn {name, sn} -> %{name: name, sn: sn} end)
4 Likes
[ "red", "green","black"] 
|> Enum.chunk_every(2)
|> Enum.into(%{}, fn [a, b] -> {a, b} end)

This would not work, @Hallski 's solution would.

But thanks now I know Enum.into accepts a mapper function too!

1 Like

Thank You… it has worked