# Max Katz

DevRel & No-Code

## 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.

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

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:

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.

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

You are ready to run the flow.

This is the main flow execution:

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

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

The third flow run:

3 + 3 = 6

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

In the fourth flow run:

4 + 6 = 10

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

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

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:

This is the 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.

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:

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.

And, this is the main flow execution showing the final 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:

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

Watch examples video: