HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to ElixirVariablesData TypesOperatorsStringsListsConditions (if / else)Iteration (Enum)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Enum
🔀 Condition
🧩 Function
Elixir›Lists

Lists

List creation, element access (Enum.at), length, and appending (++).

Simple enough for a 5-year-old to understand

Create and access a list

Items are created with square brackets. Element access is via Enum.at(items, index) (starts from 0).

Example code
1
languages = ["Elixir", "Go", "Rust"]
A list with three elements.
VariableA list with three elements.
2
IO.puts(Enum.at(languages, 0))
Elixir (first element)
Output / InputElixir (first element)
3
IO.puts(length(languages))
3 (number of elements)
Output / Input3 (number of elements)
VariableOutput / Input
💡 Hint: Enum.at(items, 0) gives the first element (0-indexed).

About this topic

In Elixir, a list has a linked structure: each element points to the next one. Therefore, prepending is O(1), while appending is expensive because it requires traversing the entire list.

Almost all operations on lists are in the Enum module: mapping, filtering, reducing, sorting. Instead of writing loops, you chain these functions; combined with the pipe operator, the code turns into a readable transformation pipeline.

There is a special syntax that separates the head and tail of a list. When combined with pattern matching, it allows you to write recursive operations very naturally; this is the classic form of list-processing functions in Elixir.

What you will learn

  • List creation
  • Head and tail separation
  • The cost of prepending and appending
  • Enum functions
  • Safe access

Who is this for?

  • Developers who will work with collections
  • Those who access by index out of array habit
  • Those experiencing performance issues

When to use?

All sequential data processing is done with lists; data transformation pipelines are built upon them.

If random access or key-based lookup is required, a list is the wrong choice; maps should be used.

Common mistakes

Wrong

liste = [1, 2, 3]
yeni = liste ++ [4]

There is no error here, but the cost is hidden: appending to the end copies the entire list. If you append to the end at every step inside a loop, the number of operations grows quadratically with the number of elements. The correct pattern is to prepend and reverse at the end.

Correct

liste = [1, 2, 3]
yeni = [4 | liste] |> Enum.reverse()

Wrong

liste = [1, 2, 3]
IO.puts hd([])

An empty list has no head, and this call stops the program with an ArgumentError. When processing lists, you always need to account for the empty state; pattern matching allows you to do this check naturally.

Correct

liste = []
case liste do
  [bas | _] -> IO.puts bas
  [] -> IO.puts "empty"
end

Wrong

liste = [1, 2, 3]
IO.puts liste[0]

Lists are not indexed with square brackets; this syntax is for keyword lists and does not work with numerical indices. To reach an element at a specific position, the access function of the Enum module is used — but this also counts from the beginning and should not be used frequently.

Correct

liste = [1, 2, 3]
IO.puts Enum.at(liste, 0)

Frequently asked questions

What is the purpose of the head and tail syntax?

It splits the list into two parts: the first element and the rest. When used in pattern matching, it forms the basis of recursive functions; you process one element at a time until the list is empty.

What is the difference between Enum and Stream?

Enum performs operations immediately and produces intermediate lists at each step; Stream is lazy, combining operations and applying them in a single pass. With large datasets, Stream significantly reduces memory usage.

What is a keyword list?

It is a list where each element is a tuple consisting of an atom and a value. It is used to pass options to functions; it allows the same key to appear multiple times and preserves its order. It differs from maps in these aspects.

Summary

Lists have a linked structure.

Prepending is cheap, appending is expensive.

Trying to get the head of an empty list throws an error.

Lists are not accessed by numerical index.

The Enum module contains list operations.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code