Accessing items in an array

As I understand it, arrays in Grasshopper are itemized as a single list. Is there a way to access the items in an array as an array, e.g. [0,0], [0,3], OR in the case of a 3D array as [1,2,3] and so on? If so, which component do we use to access them?

There are different ways to achieve this: (20.7 KB)

P.S. If you want to learn more about Data Tree in grasshopper:

1 Like

HI Mahdiyar, thank you very much for this link. What I dont understand is why the data tree is represented as {0;0;0} N=13, rather than just [0,0], [0.1], [0,2] and so on. Why is it necessary to add the two zeros before each list in the array? Later on, when a second array is added, the identifier becomes {0;1;0} rather than add a new identifier e.g. 1[0,1], 1[0,2], 1[0,3] and so on…

I mean, isn’t this how we present array variables in other computer programming languages, e.g. A[8,8] and so on? I think this latter way of representing an array is clearer and easier to use. I am trying to understand why Rhino is written differently, and whether it is possible to access arrays using the format used in other programming languages.

Data is not stored in nested or multi-dimensional arrays in Grasshopper, but in ‘data trees’. If you’re already familiar with collection types in other languages then you can think of trees as Dictionary<Path, List<T>>. The path is an ordered sequence of one or more non-negative integers. Paths are typically formatted using curly braces and semi-colons: {0} or {0;1} or {0;2;4;7;0;0;3}.

The reason for this is that some lists are more closely related than others, and this relatedness needs to be encoded into the data structure. For example a tree with four branches and three values per branch might look like this:

{0;0} (a, b, c)
{0;1} (i, j, k)
{0;2} (u, v, w)
{0;3} (x, y, z)

or like this:

{0;0} (a, b, c)
{0;1} (i, j, k)
{1;0} (u, v, w)
{1;1} (x, y, z)

Even though both trees store the exact same values, they nevertheless add a different context to these values. In the first tree all four branches are on equal footing. They all differ from all other branches by a single path element only. In fact since they all share a leading zero you could even get rid of this first element without destroying any information.

The branches in the second tree however clearly come in pairs. The first two are more related to each other then they are the last two. In this tree no path elements can be removed without consequences.

1 Like

Hi David, this is very valuable. Thank you for the explanation. Your reference to Dictionary is very useful and helps me to understand why the information is kept as data trees. Having said that, is there an easy way in which a data tree can be converted into an array type list? I ask because this will make it easier for us, as architects to reference elements in a tower, say, comprising of various units stacked on top of one another. Since many buildings are organised this way, with several units on each floor and extending up to different floors, array lists are very convenient and easy for us to correlate to the building.

I understand why a data tree might be more powerful and useful in a more open-ended mathematical organisation but architectural buildings are more simply organised.

Thank you once again for this quick explanation, boonlay.

Not particularly easy for GH1 trees, you’ll have to write a short loop yourself. In GH2 I made sure that trees can be directly converted to and from nested arrays.

However there may be new problems with this conversion in the future, as trees in GH2 are capable of storing null values for value-type collections, as well as metadata. To technically when converting a GH2 tree into nested arrays, you may end up with three nested array structures. One containing the values, one containing booleans indicating whether the values are null or not, and one containing metadata.

I think you’ll find that as you understand data trees more deeply, they actually relate quite nicely to architectural hierarchies, and have almost the same power as multi-dimensional arrays (in many cases, more power!). I think of the branch paths as records of the model’s hierarchy. Here’s an example:

The power in data trees is the way in which complex structures can be matched to one another — without having to write complex nested loops. See this thread for a more in-depth discussion: Data doubts


Hi Andrew, many thanks for this elaboration. You’re right. One of the problems here is my lack of experience with Grasshopper and data trees. The explanations given so far have been tremendously helpful in helping me more ahead and I am becoming a convert, lol. I will study your suggestion more deeply and educate myself. Many thanks, boonlay.