liveBook · Manning

Keyword list

As explained in Elixir in Action, Third Edition

A keyword list is an Elixir list whose elements are two-element tuples with atom keys and arbitrary values.

A keyword list is a list of two-element tuples whose keys are atoms and whose values can have any type. It suits small key-value structures and optional function arguments. section 2.4.11

Use keyword lists when:

  • You need readable key-value pairs such as [monday: 1, tuesday: 2]. section 2.4.11
  • You want optional arguments, including the trailing keyword-list syntax accepted by functions such as IO.inspect/2. section 2.4.11
  • You can accept linear lookup, because fetching a key has O(n) complexity. section 2.4.11

Inspect options

IO.inspect([100, 200, 300], width: 3, limit: 1) prints a constrained representation. The same options can be written as a keyword-list argument. section 2.4.11

How Elixir in Action, Third Edition covers it

How deep it goes
Defines the representation, access methods, and O(n) lookup cost without descending into implementation internals.
Examples
Uses day mappings, Keyword.get/2, bracket access, and IO.inspect formatting options.
What it assumes
Assumes familiarity with lists, tuples, atoms, functions, and maps while reinforcing their relationships.
What you can do after
Apply keyword lists to small key-value data and pass readable optional arguments to functions.
How it connects
Connects lists, tuples, atoms, maps, the Keyword module, and optional function arguments.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.