Skip to main content
TopMiniSite

Back to all posts

How to Reduce List Of Maps In Elixir?

Published on
4 min read
How to Reduce List Of Maps In Elixir? image

Best Elixir Guides to Buy in October 2025

1 Elixir in Action, Third Edition

Elixir in Action, Third Edition

BUY & SAVE
$47.40 $59.99
Save 21%
Elixir in Action, Third Edition
2 Programming Elixir ≥ 1.6: Functional |> Concurrent |> Pragmatic |> Fun

Programming Elixir ≥ 1.6: Functional |> Concurrent |> Pragmatic |> Fun

BUY & SAVE
$32.87 $47.95
Save 31%
Programming Elixir ≥ 1.6: Functional |> Concurrent |> Pragmatic |> Fun
3 Elixir Patterns: The essential BEAM handbook for the busy developer

Elixir Patterns: The essential BEAM handbook for the busy developer

BUY & SAVE
$49.00
Elixir Patterns: The essential BEAM handbook for the busy developer
4 Elixir Programming Mastery: An In-Depth Exploration for Developers

Elixir Programming Mastery: An In-Depth Exploration for Developers

BUY & SAVE
$29.99
Elixir Programming Mastery: An In-Depth Exploration for Developers
5 The Art of Elixir: elegant, functional programming

The Art of Elixir: elegant, functional programming

BUY & SAVE
$39.99
The Art of Elixir: elegant, functional programming
6 Engineering Elixir Applications: Navigate Each Stage of Software Delivery with Confidence

Engineering Elixir Applications: Navigate Each Stage of Software Delivery with Confidence

BUY & SAVE
$60.71
Engineering Elixir Applications: Navigate Each Stage of Software Delivery with Confidence
7 Designing Elixir Systems With OTP: Write Highly Scalable, Self-healing Software with Layers

Designing Elixir Systems With OTP: Write Highly Scalable, Self-healing Software with Layers

BUY & SAVE
$41.00
Designing Elixir Systems With OTP: Write Highly Scalable, Self-healing Software with Layers
8 Introducing Elixir: Getting Started in Functional Programming

Introducing Elixir: Getting Started in Functional Programming

BUY & SAVE
$18.49 $24.99
Save 26%
Introducing Elixir: Getting Started in Functional Programming
9 Metaprogramming Elixir: Write Less Code, Get More Done (and Have Fun!)

Metaprogramming Elixir: Write Less Code, Get More Done (and Have Fun!)

BUY & SAVE
$17.00
Metaprogramming Elixir: Write Less Code, Get More Done (and Have Fun!)
10 Elixir in Action

Elixir in Action

BUY & SAVE
$49.99
Elixir in Action
+
ONE MORE?

In Elixir, we can reduce a list of maps using the Enum.reduce/2 function. This function allows us to iterate over each element in the list and perform a specific operation on each map. By providing an initial accumulator value and a function to combine each map with the accumulator, we can effectively reduce the list to a single value.

For example, if we have a list of maps representing sales data and we want to calculate the total revenue, we can use Enum.reduce/2 to sum up the revenue of each map in the list.

Here is an example code snippet:

sales_data = [%{product: "A", revenue: 100}, %{product: "B", revenue: 200}, %{product: "C", revenue: 150}]

total_revenue = Enum.reduce(sales_data, 0, fn map, acc -> acc + map[:revenue] end)

IO.puts("Total revenue: #{total_revenue}")

In this example, we start with an initial accumulator value of 0 and add the revenue of each map to the accumulator. Finally, we print out the total revenue after reducing the list of maps.

The recommended strategy for sorting a list of maps by a specific key in Elixir is to use the Enum.sort_by/3 function combined with the Map.get/2 function to extract the value of the desired key for each map.

Here is an example implementation:

list_of_maps = [%{key: 3}, %{key: 1}, %{key: 2}]

sorted_list = Enum.sort_by(list_of_maps, &Map.get(&1, :key))

IO.inspect(sorted_list)

In this example, list_of_maps is a list of maps with a key key that we want to sort the maps by. We use the Enum.sort_by/3 function to sort the list based on the value of the key key in each map. The Map.get/2 function is used in the sorting function &Map.get(&1, :key) to extract the value of the key key for each map.

This will result in sorted_list containing the list of maps sorted in ascending order based on the value of the key key.

How to convert a list of maps to a single map in Elixir?

You can convert a list of maps to a single map in Elixir by using the Enum.reduce/2 function. Here's an example of how you can achieve this:

list_of_maps = [%{key1: "value1"}, %{key2: "value2"}, %{key3: "value3"}]

resulting_map = Enum.reduce(list_of_maps, %{}, fn map, acc -> Map.merge(acc, map) end)

IO.inspect(resulting_map)

In this example, we start with an empty map (%{}) as the initial accumulator and then use Enum.reduce/2 to iterate over the list of maps. In each iteration, we merge the current map with the accumulator using Map.merge/2, which combines the key-value pairs from both maps. Finally, we get a single map that contains all the key-value pairs from the list of maps.

How to iterate over a list of maps in Elixir?

To iterate over a list of maps in Elixir, you can use the Enum module's each/2 function or the Enum.each/2 function. Here's an example of how you can do this:

list_of_maps = [%{key1: "value1"}, %{key2: "value2"}, %{key3: "value3"}]

Enum.each(list_of_maps, fn(map) -> IO.inspect(map) end)

In the code above, we have a list of maps list_of_maps and we are using Enum.each/2 function to iterate over each map in the list. Inside the anonymous function, we are simply printing the map using IO.inspect.

Alternatively, you can also use the each/2 function from the Enum module, which is a shorthand for Enum.each/2:

list_of_maps = [%{key1: "value1"}, %{key2: "value2"}, %{key3: "value3"}]

Enum.each(list_of_maps, &IO.inspect/1)

Both of these examples will iterate over each map in the list and print them to the console. You can replace IO.inspect with any other function you want to apply to each map in the list.

How to find the maximum value in a list of maps in Elixir?

To find the maximum value in a list of maps in Elixir, you can use the Enum.max_by/3 function along with a custom comparison function. Here's an example:

list_of_maps = [ %{key: 1, value: 10}, %{key: 2, value: 20}, %{key: 3, value: 15} ]

max_value = Enum.max_by(list_of_maps, &(&1[:value]))

IO.inspect(max_value)

In this example, we have a list of maps where each map has a :value field. We use Enum.max_by/3 to find the map with the maximum :value field. The second argument &(&1[:value]) is a custom comparison function that extracts the :value field from each map for comparison.

After running this code, the max_value variable will contain the map with the maximum :value field, which in this case is %{key: 2, value: 20}.