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




















