liveBook · Manning

List

As explained in Elixir in Action, Third Edition

A list is an ordered Elixir data structure whose elements can be traversed and combined through list operations.

Use a list for an ordered, dynamically sized collection when traversal and sequential construction matter more than direct element access. Elixir lists are singly linked structures: most operations, including length, indexed lookup, arbitrary replacement, insertion, and concatenation, require traversal and typically cost O(n). Choose tuples, maps, or a higher-level structure when direct access is the real requirement. section 2.4.4 section 2.8

Work with lists through the List and Enum modules, while remembering that transformations return new values:

  • Query elements with Enum.at/2, test membership with in, and use List functions for insertion or replacement. section 2.4.4
  • Treat list updates as immutable transformations: rebind the returned list when you need the changed version. section 2.4.5 section 4.0
  • Build lists by pushing elements onto the head in O(1), then reverse once when the desired order is complete. section 2.4.5
  • Use ++ for concatenation only with its traversal cost in mind; appending to the ordinary tail remains expensive. section 2.4.4

Constructing and updating a list

Start with prime_numbers = [2, 3, 5, 7]. Enum.at(prime_numbers, 3) returns 7, while 5 in prime_numbers returns true. List.replace_at(prime_numbers, 0, 11) returns [11, 3, 5, 7] without changing the original binding; store or rebind that result when continuing. Prepending with [:new_element | prime_numbers] adds an element efficiently to the front. section 2.4.4 section 2.4.5

a_list = [head | tail]

Destructure a list as [head | tail] to expose its first element and the remaining list, the core shape behind recursive traversal and efficient prepending. section 2.4.4

An IO list applies the same nested-list idea to output. Its leaves are bytes, binaries, or other IO lists, allowing incremental construction through nesting instead of repeated ordinary-list appends. I/O functions can consume the structure directly for files, sockets, or screen output. section 2.4.12

How Elixir in Action, Third Edition covers it

How deep it goes
Explains linked structure, immutability, sharing, operation costs, head-tail construction, and IO-list optimization.
Examples
Uses prime numbers, membership checks, indexed lookup, replacement, head-tail patterns, and nested output construction.
What it assumes
Assumes basic Elixir syntax and variables while teaching list representation, immutable updates, and module-based operations.
What you can do after
Apply it via List, Enum, pattern matching, and IO functions while choosing operations according to their traversal costs.
How it connects
Connects lists with the List module, Enum, immutability, tuples, maps, singly linked lists, and IO lists.

Also explained in

Compare how 21 books explain list →

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.