How to Reduce a List to a Single Value, 3 Examples Using List-Reduce Workflows Card

Okta Workflows how-to guides are questions and answers from weekly community office hours, MacAdmins Workflows Slack channel, and other places. Read all other how-to guides

On to the question. 

How to use the List-Reduce card in Workflows?

The List-Reduce card takes a list of values and reduces the list to a single value.

Here are a number of examples where you can use the List-Reduce card to reduce a list of values to a single value:

  • A list of numbers – sum all the numbers
  • A list of numbers – find the largest (or smallest) number
  • A list of messages (text) – create a single message containing all the messages from a list

A good way to understand how this card works is to look at an example to return the sum of all the numbers in a list.

At the end of this blog post, you can download the examples.

Sum of all numbers in a list

Starting with a list of numbers, you want to sum up all the numbers.

A list with numbers

In order to sum up the numbers, you are going to use the List-Reduce card that requires a helper flow.

A flow with a List-Reduce card

Before creating the helper flow, this is how the helper flow works (each bullet point is helper flow running once):

  • 0 (memo, initial value) + 1 = 1
  • 1 + 2 = 3
  • 3 + 3 = 6
  • 6 + 4 = 10
  • 10 + 5 = 15

The List-Reduce card walks through the list number-by-number, at each step adding the current number to the sum from the previous step.

The result of this flow is 15.

The list of numbers was reduced to a single value of 15.

Now let’s build the helper flow so you can set it in the List-Reduce card.

This is how the helper flow looks:

Reduce helper flow

The first card, On Demand-Helper Flow, makes this flow a helper flow. It has two inputs:

  • current number – the current number from the list
  • memo – holds the initial value when the flow runs for the first time and then stores and passes in the sum after each run

The second card, Number-Add, adds two numbers. It adds the current number from the list with memo – which is the previous sum.

The last card, Flow Control-Return, returns the current sum. This sum value is passed back into the helper flow using the memo field.

The helper flow continues to run until all the list numbers are processed.

Now that the helper flow is completed, you can set it on the List-Reduce card. After choosing a helper flow, set the current number field value. Click the arrow pointing down and select item.

A flow with a List-Reduce card

Two things to remember:

  • Set the memo field to type Number. It holds the initial and intermediate sum values when the helper flow runs. Its type should match the type you expect as the final result
  • Set the item (result) to type Number
Setting result type

You are ready to run the flow.

This is the main flow execution:

Running the main flow

A list of [1,2,3,4,5] is passed to the List-Reduce card. The final sum is 15 (item field).

Now let’s look at the helper flow executions.

In the first run, the flow is adding:

1 + 0 = 1

  • 1 – current number from the list
  • 0 – memo
First run

In the second run, the flow is adding:

2 + 1 = 3

  • 2 – current number from the list
  • 1 – sum from the previous run, passed with memo field
Second run

The third flow run:

3 + 3 = 6

  • 3 – current number from the list
  • 3 – sum from the previous run, passed with memo field
Third run

In the fourth flow run:

4 + 6 = 10

  • 4 – current number from the list
  • 6 – sum from the previous run, passed with memo field
Fourth run

In the fifth and final run:

5 + 10 = 15

  • 5 – current number from the list
  • 10 – sum from the previous run, passed with memo field
Fifth run

This example showed you how to sum up a list of numbers (or, reduce a list of numbers to a single value).

Let’s look at more examples that use the List-Reduce card.

Find the largest number in a list of numbers

In this example, you have a number list and want to find the largest number in the list. This is the main flow:

A flow to find the largest number

This is the helper flow:

Finding the largest number – helper flow

In this flow, the Branching-Assign If card is used to compare two numbers, and the bigger number is returned back as the memo value for the next run.

Then the new current number is compared against the current biggest number (memo). The bigger of them is returned. This continues until all the numbers are processed.

The final result where the biggest number is 60.

Finding the largest number

Let’s look at another example.

Creating a single message from multiple messages

This example creates a single message from multiple messages. For example, there is a list of users who were activated. You need to create one message that includes all the messages/users.

This is the main flow:

Create single message flow

This is the helper flow:

A helper flow to create a single message from multiple messages

The following screenshot shows the helper flow 2nd execution. It shows how the message is built.

Helper flow

And, this is the main flow execution showing the final message:

Using List – Reduce card to build a message

In this blog post you learned how to use the List-Reduce card using three examples:

  • A list of numbers – sum all the numbers
  • A list of numbers – find the largest (or smallest) number
  • A list of messages (text) – creating a single message from multiple messages

Try these examples:

  1. Download the flows file
  2. Uncompress the file (you should get reduce.folder file)
  3. Import reduce.folder into Workflows

Watch examples video:

Using the List – Reduce card video

More resources to help you learn:

 📺 Short how-to Workflows videos to help you become a better automation builder.

 🍿 A collection of helpful Workflows tips.

Published by

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.