How to Sum Elements of a List in Grasshopper

How to Sum Elements in a List in Grasshopper - Full Tutorial
Quick links

Have you ever found yourself staring at a list of elements, wondering how to sum them for a total? You’re not alone. In this post I’ll walk you through how to sum elements of a list in Grasshopper, and what to watch out for to make your project calculations smooth and hassle-free.

Let’s dive in!

The Mass Addition Component

The key to adding any list of numbers in Grasshopper, is the Mass Addition component. This component takes any list of numbers and returns the total sum. Let’s learn where to find it and what to watch out for to make sure it works as intended!

Let’s start by where to find the component. The Mass Addition component is located in the ‘Maths‘ component tab, grouped under ‘Operators‘. The icon shows a cloud of addition symbols.

Location of the Mass Addition Component

To add it to your script without having to click through the component tabs, double-click where you want to component to be placed, and type ‘mass addition‘ into the component search bar that appears. Then select the first result to add the component.

Quick Adding the Mass Addition Component in Grasshopper

Let’s take a closer look at the inputs and outputs of this component.

Inputs and Outputs of the Mass Addition component explained

The input is quite straightforward: any list of numbers.

The Mass Addition component returns two outputs from that list: the Results (R) output contains the sum of all the elements in the list. The Partial Results (Pr) output on the other hand returns the partial results of this “mass addition”.

Example of how to sum elements of a list in Grasshopper with the Mass Addition component

Now what do these partial results represent?

The are a list of all the intermediate steps leading up to the total sum.

Imagine you have a list of ten numbers from 1 to 10. If you were to add them all together, you would start by adding 1 and 2, which results in 3. Then you would add 3 to make it 6. Next you’d add 4 to make it 10. The partial results output contains all the intermediate addition steps that lead up to the final sum. This can be quite useful in some situations.

Understanding the result and partial result of the mass addition component

While the component is quite straightforward, there are some things to keep in mind.

How Data Structure Influences Summing Results

When using the Mass Addition component in Grasshopper, it’s important to understand that this component sums up the elements of a list. In Grasshopper, a list is not a generic term, it describes a particular type of data structure.

Let me show how different data structures lead to different results:

Example 1: Sum of Elements in a List

In the example below I’m using the Panel component to both display the contents and the structure of the data. Here the data is stored in single list or branch, which means the Mass Addition component processes the information as expected.

Example 2: Sum of Elements in Separate Data Branches

In this second example we have the same numbers, but they are grouped in three different data branches, making up three separate lists. Data branches exist to keep buckets of data separate. This also means that the Mass Addition component won’t ‘cross’ the line of one branch to the next, which is why it returns three separate sums, one for each list.

Example 3: Getting the Total Sum of Elements Across All Data Branches

Now sometimes we have numbers stored in several data branches, but we still want to get the total sum of all the numbers. To do that, we need to effectively ‘delete’ the data structure information and bring all the numbers back to the same list. To do that in Grasshopper, we have to use the ‘Flatten’ component. Flattening a data tree means that all branch information will be erased, like felling a tree.

To flatten the input data, right-click on the Input List (I) input of the Mass Addition component, and select ‘Flatten’.

Once we flatten the data, the Mass Addition component will return a single number, representing the sum of all elements in the list.

The small downward pointing arrow on the input give us a visual cue that we flattened the data.

And as expected, we get the total sum of all the numbers across the branches in the Result (R) output.

Concluding Thoughts: Mass Addition in Grasshopper

In summary, mastering the Mass Addition component is a key step in enhancing your Grasshopper skills, enabling you to sum elements of a list effectively. Always make sure you check the data structure to avoid surprises and get the result you want!

Happy designing!

Like this article?
Share on LinkedIn
Picture of Thomas Tait
Thomas Tait
Architect at Snøhetta and Head Instructor @ Hopific (or, in plain English, I help designers use Grasshopper to supercharge their designs.
Ready to Dive into Grasshopper? Grab Your Free Ebook!

Accelerate Your Learning and Get Up and Running Quickly.