List comprehension in Elixir

Key takeaways:

  • Elixir is a functional, dynamically typed programming language.

  • Lists in Elixir are immutable and implemented using linked lists.

  • List comprehensions generate new lists by applying operations to existing lists.

  • Syntax includes for, pattern, enumerable, filter, and do.

  • List comprehensions improve code readability and facilitate efficient list manipulation.

Elixir is a functional, dynamically typed, high-level, general-purpose programming language first released in 2011. When working with data structures, Elixir provides numerous built-in data types, making it easy to handle and manipulate data.

Lists

Lists are used to store collections of values of the same or different types. Lists are immutable and implemented using linked lists. Elixir supports list comprehensions, which concisely generate new lists by applying some operation/function to each element of an existing list.

Basic syntax of list comprehension

The basic syntax of list comprehension is given below:

newlist = for pattern <- enumerable, filter, do: expression
  • for: It is used to start a comprehension.

  • pattern: It is a variable used to take values from given enumerable one by one.

  • enumerable: It is the list on which we perform some operation.

  • filter: It is an optional parameter that includes or excludes elements from the enumerable based on requirement.

  • do: expression: It is the operation to be performed on each element of the enumerable.

The above syntax is explained through the illustration below:

Illustration of comprehension syntax
Illustration of comprehension syntax

Basic list comprehension example

The code example of comprehension is given below:

list_1 = [1, 2, 3, 4, 5, 6, 7, 8]
IO.puts("Input list:")
IO.inspect(list_1)
list_2 = for n <- list_1, do: n / 2
IO.puts("Resultant list:")
IO.inspect(list_2)

Explanation:

  • Line 1: Define a list.

  • Lines 2–3: Print the input list values.

  • Line 4: Perform list comprehension on list_1 by dividing each element by 2.

  • Lines 5–6: Print the resultant list values.

List comprehension with filter

The code example of comprehension is given below with a filter, where we filter out even values and perform a square on those even values:

list_1 = [1, 2, 3, 4, 5, 6, 7, 8]
IO.puts("Input list:")
IO.inspect(list_1)
list_2 = for n <- list_1, rem(n, 2) == 0, do: n * n
IO.puts("Resultant list:")
IO.inspect(list_2)

Explanation:

  • Line 1: Define a list.

  • Lines 2–3: Print the input list values.

  • Line 4: Perform list comprehension on even elements of list_1 by squaring its values.

  • Lines 5–6: Print the resultant list values.

List comprehension with multiple filters

The code example of comprehension is given below with two filters based on even and odd values, where the even values are squared and the odd numbers are halved:

list_1 = [1, 2, 3, 4, 5, 6, 7, 8]
IO.puts("Input list:")
IO.inspect(list_1)
list_2 = for n <- list_1, do: if rem(n, 2) == 0, do: n * n, else: n / 2.0
IO.puts("Resultant list:")
IO.inspect(list_2)

Explanation:

  • Line 1: Define a list.

  • Lines 2–3: Print the input list values.

  • Line 4: Perform list comprehension on list_1.

  • Lines 5–6: Print the resultant list values.

List comprehension with into operation

The following code example shows how the into operation is used with for operator in Elixir. The into operator returns the result of list comprehensions as a different data structure. The code snippets below convert the list of integers to characters using into operator.

defmodule SomeMod do
def string do
for c <- [65, 110, 115, 119, 101, 114], into: "", do: <<c>>
end
end
IO.inspect SomeMod.string

Explanation:

  • Line 1: Define a module named SomeMod.

  • Line 2: Define a function string inside a module.

  • Line 3: Perform list comprehension by converting a list of integers to a string.

  • Line 6: Print the result.

Conclusion

In Elixir, list comprehensions provide a concise and powerful way to manipulate lists. They provide a clean and readable syntax to perform various operations such as filtering, mapping, combining lists, etc.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is the expression in list comprehension?

The expression in list comprehension is the operation or transformation applied to each element of the iterable, usually following the do keyword.


When should we use list comprehension?

Use list comprehension when we need to generate a new list from an existing iterable, especially when applying a simple transformation or filter, as it enhances code readability.


Can we use else in list comprehension?

No, Elixir does not support an else clause directly within list comprehensions. We can use conditional filters instead to achieve similar functionality.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved