Split a List into Every Nth Element
A quick way to split a single list into groups continining every nth element.
One thing I sometimes find myself doing when working with data lists in Grasshopper is splitting a list so that each nth element is in a different list. For instance, say I have a spiral of points, with the points being equally-spaced on a series of angles (eg. 60º, 120º, 180º, etc). The points have been created by feeding a Point Polar with a range of angles and a range of distances. The result is that the points describe the spiral in one long list with no knowledge of the fact that successive rotations lie on the same angles. If I wanted to work with the points that lie on each radiating line individually, then I’d need to extract every nth element - in this case, every 6th element. So, for the case below, I’d need to create a list of points 1, 7 and 13 and another list of 2, 8 and 14, and so on. How can this be done?
To solve this problem, we could use Partition List (Sets → List → Partition List), followed by a Flip Matrix (Sets → Tree → Flip Matrix). The partition list takes the first n number of elements and creates a branch, then the next n number of elements and puts those into the next branch and so on through the full data set. This doesn’t achieve what we want, but it gets us closer to where we want to go. If we could take the first element of each tree branch and group them, and then do the same with the second element, and so on, then we’d have the results we’re after. Flip Matrix does exactly this. Think of Flip Matrix like flipping a grid. If we think of branches like columns in a grid, then the output of the Partition function results in 3 columns, each with 6 elements in them. When we flip the matrix, we end up rotating that grid so that we now have 6 columns, each with 3 elements contained - exactly what we’re after.
Let’s look at an example with numbers. Let’s begin with a series, 1 to 18 that we want to split into groups that contain every 6th element in them. So we start with 1, 2, 3, 4, 5, 6… and we end up with groups that look more like 1, 7, 13.
The first panel here shows the initial starting set. The data is created using a Series (Sets → Sequence → Series) node. This node takes 3 inputs:
- S - Start - the first number in the series
- N - Step - the step size for each successive number
- C - Count - the number of values in the series
So in this case, to generate a list of numbers from 1 to 18, we supply 1 as S, the first element, 1 as N, the step between elements, and 18 as C, the number of elements.
The series from 1 to 18 is fed into the Partition List node, along with the size of each group in the partition, 6. The output of the Partition can be seen in the second panel of the above diagram. If we represent each branch as a column in a grid, we can represent these numbers as in the following image:
As you can see, the first 6 numbers have been placed in the first column (branch), the next 6 in the next column, and the final 6 in the last column.
Our ultimate goal is essentially to take the first element of each column and put those in their own branch, then take the second element of each column and put those in their own branch, and continue this until there are no more elements in each column. The node to do this is called Flip Matrix (Sets → Tree → Flip Matrix). The output of flip matrix can be seen in the third panel of the Grasshopper example above. Representing the branches as columns again, we get something that looks like this:
So every 6th element is split into their own groups (branches). For the example outlined above, this then means that each branch holds the points that have the same angle in common.