To work with lists in Grasshopper, there is a separate section in the menu: Sets / List
Let's take a look at a few nodes from this list.
<aside>
🟩 Select a specific item from the list using the List Item
node.
</aside>
<aside> 📌 To select the last item from the list, you can use the value "i" -1 and regardless of the list length, we will get the last value.
![Untitled] (https://prod-files-secure.s3.us-west-2.amazonaws.com/ee848b0b-0549-4363-9009-29b090347be4/97249513-a224-4b11-8ba7-17725de57b96/Untitled.png)
</aside>
<aside>
🟩 The length of the list is determined using the List Length
node.
![Untitled] (https://prod-files-secure.s3.us-west-2.amazonaws.com/ee848b0b-0549-4363-9009-29b090347be4/77d8c47b-932e-432a-b422-c4cd8e8ce905/Untitled.png)
</aside>
<aside> ⚠️ Note that the ordinal number of the list starts from zero, so 5 is the sixth value of the list!
</aside>
<aside>
☝ In the examples above, we manually specify the number of values in the list, so if the number of points changes, we will need to change the value of the list length in the Series
or Random
node. This can be automated using the List Length
node, which calculates the length of the list connected to the input "L" and returns this value to the output "L", so now when the number of points changes, the script will work correctly.
![Untitled] (https://prod-files-secure.s3.us-west-2.amazonaws.com/ee848b0b-0549-4363-9009-29b090347be4/0d490ac7-466a-4290-b850-01a1cac2bb95/Untitled.png)
</aside>
<aside>
🟩 The Reverse List
node reverses the list, with the last item becoming the first and vice versa.
![Untitled] (https://prod-files-secure.s3.us-west-2.amazonaws.com/ee848b0b-0549-4363-9009-29b090347be4/8ecfdcbf-9ed1-432f-8c3a-4ad4a2d2737d/Untitled.png)
</aside>
<aside>
🟩 The Sort List
node sorts numerical values from smallest to largest.
![Untitled] (https://prod-files-secure.s3.us-west-2.amazonaws.com/ee848b0b-0549-4363-9009-29b090347be4/276d3a88-c61b-4841-9a58-a411332c1845/Untitled.png)
This node allows you to sort multiple lists with different data but with the same list length. For example, we can have a set of rectangles and we need to sort them by area.
![Untitled] (https://prod-files-secure.s3.us-west-2.amazonaws.com/ee848b0b-0549-4363-9009-29b090347be4/71a433d9-d1d9-4601-9568-8e82457235ba/Untitled.png)
the Area
node calculates the area
![Untitled] (https://prod-files-secure.s3.us-west-2.amazonaws.com/ee848b0b-0549-4363-9009-29b090347be4/288ce657-ffce-49c8-899a-911ce7734d94/Untitled.png)
now the Sort List
node
![Untitled] (https://prod-files-secure.s3.us-west-2.amazonaws.com/ee848b0b-0549-4363-9009-29b090347be4/70304e7e-12f3-4650-b1af-9d2c4e0f5c85/Untitled.png)
But we need to sort the rectangles themselves for this, and there is a second input. This node sorts any other list based on the keys "K", which must be numbers...
![Untitled] (https://prod-files-secure.s3.us-west-2.amazonaws.com/ee848b0b-0549-4363-9009-29b090347be4/78ed085b-9799-4c7c-a96a-266341869986/Untitled.png)
And to check the result, let's select the largest rectangle, using the List Item
node with the value "i" -1
![Untitled] (https://prod-files-secure.s3.us-west-2.amazonaws.com/ee848b0b-0549-4363-9009-29b090347be4/7683f925-15b2-4994-a98a-68e4a1e07080/Untitled.png)
</aside>
<aside>
📌 Sort List
can sort multiple lists at once. To do this, hover over this node and click on the "+" that appears on it.
</aside>