elixir list map key string to atom

To convert a string key to an atom in Elixir, you can use the String.to_existing_atom/1 function. This function converts a string to an atom if the atom already exists in the system, or returns an error if the atom does not exist.

Here's an example of how you can use String.to_existing_atom/1 to convert a string key to an atom in a list of maps:

list = [%{key: "value1"}, %{key: "value2"}, %{key: "value3"}]

converted_list = Enum.map(list, fn %{key: key} -> %{String.to_existing_atom(key) => key} end)

In the above code, the Enum.map/2 function is used to iterate over each map in the list. Inside the map function, String.to_existing_atom/1 is called to convert the string key to an atom. The resulting atom is then used as the key in the new map, with the original string key as the value.

The converted_list variable will now contain a list of maps where the keys are atoms.