keyword lists

We’ll often want to set up a 2-item tuple as a representation of a key-value data structure.

In languages like Python, this is similar to a dictionary.

For instance, if we want to set up a key-value mapping, we set them as a list of tuples from the first item to the second. For instance:

iex> list = [{:a, 1}, {:b, 2}]
iex> list[:a] # 1
iex> list[:c] # nil

We can concatenate the dictionary using the key-value syntax woth ++. For instance:

iex> list = [{:a, 1}, {:b, 2}]
iex> list[:a] # 1
iex> list[:c] # nil
iex> newList = list ++ [c: 3]
iex> newList[:c] # 3
iex> list[:c] # nil

We’ll use keyword-lists a lot in Ecto (the database wrapper of the Phoenix library) to make elegant queries to our database. We’ll come back to this in a little while.

Keyword lists are just lists which means that this isn’t really a fantastic data structure for doing large operations against. For this reason, Elixir provides another data structure: the Map.