Intro

Keyword lists are similar to list. They are a sequence of tuples that are enclosed with square brackets [{}, {}] .

Becasue keyword lists are lists they support all the operations that can be perfomed on a list.

elixir
# Define a keyword list

keyword_list = [{:stuff, "stuff"}, {:things, "things"}]

# Special syntax to define a keyword list

keyword_list = [stuff: "stuff", things: "things"]

# Pass keyword list to function

if(false, [{:do, :this}, {:else, :that}])

# syntastic sugar version
if false, do: :this, else: :that

Considerations

  • Keys must be atoms
  • Keys are ordered based on insertion
  • Keys can be declared more than once
  • Keyword lists are the syntax for passing parameters to functions
# elixir