How to sort surfaces by z coordinates in ghpython

Hi everyone,

I have just started using ghpython as there are some things that I could not solve in grasshopper itself. The issue that I have been trying to solve for these past 2 days in python is to sort surfaces into separate lists based on their z coordinate. All the surfaces are planar. So basically what I want is that all surfaces with plane z=0 is one list, all surfaces with plane z=5 the second list…

Does anyone know how to solve this in ghpython?

See this:

Also, welcome to the forum :slight_smile: If this is about GHPython you ought change the category to either Grasshopper Developer or Scripting. And make sure to go through some tips that’ll help you getter answers.

Missed the “into separate lists” requirement yesterday, here’s one approach to solving this:


210618_SortSurfacesByZIntoLists_00.gh (14.1 KB)

1 Like

I was just looking up some old code and was reminded of the groupby function from itertools, which is another relevant approach. Here’s an example that demonstrates how to use the same key lambda function (a normal Python function will also work) to first sort then group a list of surfaces by Z:


210618_SortSurfacesByZIntoLists_01.gh (15.2 KB)

I’ll stop now :slight_smile:

3 Likes

Hi Anders,

This is fantastic! The script you made is exactly what i was looking for. Thanks for the help!

1 Like