Convert two lists of the same size into a pair of key values ​​in an elixir

I am trying to find a better way to combine two lists of the same size into a map of key value pairs.

I use the same function to handle this case for a while for CSV and raw SQL queries that return some list header along with list of strings.

This is the function I used

Enum.zip(list1, list2) |> Enum.into(%{}) 

For instance:

 # For CSVS header = ["column1","column2","column3"] rows = [["a","b","c"],["d","e","f"]] Enum.each rows, fn(row) -> # Map the header to each row field row = Enum.zip(header, row) |> Enum.into(%{}) # Do some processing with the row IO.inspect row end 

Is there a function in elixir / erlang that will do this for me, or is the aforementioned combination of zip / in a better way to do this?

+5
source share
2 answers

After discussing with several people, the method I used is the best way to map keylists to valuelists.

 Enum.zip(list1, list2) |> Enum.into(%{}) 
+12
source

I had a similar question and I asked it on the elixir-lang slack group and got an answer that is exactly the same as your approach.

What you used is a good solution. Now you must stick to it.

+4
source

Source: https://habr.com/ru/post/1239585/


All Articles