# How to convert list of list to map?

I have a list which consists of [A, B, C, D, E] and A has a list of values [1, 2, 3, 4, 5] similarly B has a list of values [1, 2, 3, 4, 5]

``````Date,SKU,Unit Price,Quantity,Total Price
2019-01-01,Death by Chocolate,180,5,900
2019-01-01,Cake Fudge,150,1,150
2019-01-01,Cake Fudge,150,1,150
2019-01-01,Cake Fudge,150,3,450
2019-01-01,Death by Chocolate,180,1,180
2019-01-01,Vanilla Double Scoop,80,3,240
2019-01-01,Butterscotch Single Scoop,60,5,300
2019-01-01,Vanilla Single Scoop,50,5,250
2019-01-01,Cake Fudge,150,5,750
``````

Something like this but what I want is to create a map where the header is equal to values corresponding to it.

The expected output looks something like this:-

``````[%{date: '2019-01-01',SKU: 'Death by Chocolate', price: 50, quantity: 3},
%{date: '2019-01-01',SKU: 'Cake Fudge', price: 150, quantity: 3}]
``````

`Enum.zip/1` and `Enum.map/2` are your friends here.

``````[[1, 2, 3, 4], ~w[one two three four]]
|> Enum.zip()
|> Enum.map(fn {number, word} ->
%{
number: number,
word: word
}
end)
``````
4 Likes

Thanks bro. Youâ€™re expert!

This looks like a perfect candidate for CSV parsing?

2 Likes

This is my output, i use `map.put` function, i know it is very silly question.

array_of_digits = [3, 5, 2, 7, 4, 2, 3]
matrix_of_sum = [21,12,17,22]

``````iex(72)> sum_of_all(array_of_digits,matrix_of_sum)
[
%{
21 => [
[3, 2, 7, 4, 2, 3],
[5, 7, 4, 2, 3],
[5, 2, 7, 4, 3],
[3, 5, 7, 4, 2],
[3, 5, 2, 7, 4]
]
},
%{
12 => [
[3, 4, 2, 3],
[7, 2, 3],
[5, 2, 2, 3],
[3, 2, 4, 3],
[5, 4, 3],
[2, 7, 3],
[3, 7, 2],
[3, 5, 2, 2],
[3, 5, 4],
[3, 2, 7],
[5, 7]
]
},
%{
17 => [
[3, 5, 4, 2, 3],
[3, 2, 7, 2, 3],
[5, 7, 2, 3],
[3, 7, 4, 3],
[3, 5, 2, 4, 3],
[5, 2, 7, 3],
[3, 5, 7, 2],
[3, 5, 2, 7]
]
},
%{22 => [[3, 5, 2, 7, 2, 3], [3, 5, 7, 4, 3]]}
]
``````

But this i want :

``````iex(3)> sum_of_all(array_of_digits, matrix_of_sum)
%{
12 => [[3, 2, 7],[3, 7, 2],[3, 4, 5],[7, 5],[3, 2, 2, 5],[3, 2, 4, 3],[2, 7, 3],[3, 4, 2, 3],[7, 2, 3],[4, 5, 3],[2, 2, 5, 3]],
17 => [[3, 2, 7, 5],[3, 7, 2, 5],[3, 4, 7, 3],[3, 2, 7, 2, 3],[3, 2, 4, 5, 3],[2, 7, 5, 3],[3, 4, 2, 5, 3],[7, 2, 5, 3]],
21 => [[3, 2, 4, 7, 5],[3, 4, 7, 2, 5],[3, 2, 4, 7, 2, 3],[2, 4, 7, 5, 3],[4, 7, 2, 5, 3]],
22 => [[3, 4, 7, 5, 3], [3, 2, 7, 2, 5, 3]]
}
``````

Donâ€™t use Enum.map, use Enum.reduceâ€¦

These are NOT arrays, but lists. They are not equivalent.

Please show the code for the function sum_of_all, we cannot guess what You wrote.

thanks

You might replace this part with Enum.reduce

``````Enum.reduce(list, %{}, fn elem, acc ->
...
end)
``````

like that or how?? it shows error ??

``````Enum.reduce(list, %{}, fn x, sum_of_one(array_of_digits, n) -> x , sum_of_one(array_of_digits, n) end)
``````

Enum.reduce is an important function, You should try to understand what it does.

``````Map.put(acc, elem, sum_of_one(array_of_digits, elem))
``````