Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert two lists of same size to key value pair in elixir

I'm trying to figure out the best way to combine two lists of the same size into a map of key value pairs.

I've been using the same function to handle this case for a while for CSVs and raw SQL queries which return some sort of header list along with row lists.

This is the function I've been using

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

For example:

# 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 above combination of zip/into the best way to do it?

like image 585
Nathaniel Johnson Avatar asked Dec 31 '15 15:12

Nathaniel Johnson


People also ask

What is the difference between First/2 and first/1 in Elixir?

Returns the first element in list or default if list is empty. first/2 has been introduced in Elixir v1.12.0, while first/1 has been available since v1.0.0. Flattens the given list of nested lists. Empty list elements are discarded. Flattens the given list of nested lists. The list tail will be added at the end of the flattened list.

How to convert lists of different length key value pair to dictionary?

Convert lists of different length key value pair to dictionary We are using the zip_longest () function defined in the itertools module as per python doc zip “ Make an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue.

How do I concatenate two lists in Elixir?

Lists in Elixir are specified between square brackets: Two lists can be concatenated and subtracted using the Kernel.++/2 and Kernel.--/2 operators: An element can be prepended to a list using |:

How do I prepend an element to a list in Elixir?

An element can be prepended to a list using |: Lists in Elixir are effectively linked lists, which means they are internally represented in pairs containing the head and the tail of a list: Similarly, we could write the list [1, 2, 3] using only such pairs (called cons cells):


2 Answers

After discussing with a few people the method I was using is the best way to accomplish mapping lists of keys to lists of values.

Enum.zip(list1, list2) |> Enum.into(%{})
like image 166
Nathaniel Johnson Avatar answered Oct 21 '22 23:10

Nathaniel Johnson


I was having a similar question and i asked it on elixir-lang slack group and got a answer that is exactly like your approach.

What you used is a good solution.For now you have to stick to it.

like image 22
coderVishal Avatar answered Oct 21 '22 23:10

coderVishal