Using GHPython in Grasshopper is like giving your designs an extra boost. It lets you add Python code to your Grasshopper visuals. While Grasshopper is about connecting pieces to design, GHPython lets you add your own special touch with Python scripts

Maybe you sometimes feel overwhelmed by the many wires and components needed for evaluations, comparisons, and filtering in Grasshopper scripts? Spaghetti can be tasty but not in every case!
While a simple Expression component can solve basic cases, there are more advanced tools available. By inserting a few lines of code into the GH Python component, you can create a single component that handles complex evaluations, complete with comments to clarify the process. It's similar to a cluster but quicker to write and simpler to maintain.
Another limitation Grasshopper users often encounter after a while is the downstream dataflow. Each component is executed before sending outputting the data to the next components.
But how can we iterate over the same data multiple times?
Known as looping. There are plugins solving this using Grasshopper components only, such as Anemone. However, when sharing scripts with colleagues, classmates, and others interested, it is best practice to try to use as few external plugins as possible. Increasing reusability and making it easier for others to use your scripts without having to download additional packages.
Below is a snippet showing how a list of breps and meshes are sorted by their volume and separated into groups of Breps and Meshes, and also how the same can be obtained in Python by a few lines of code.

Iterating and filtering with the GH Python component
Code to Python script:
"""Provides a scripting component.
Inputs:
geometry: List of breps and meshes to be filtered and sorted
Output:
Brep: Breps sorted in ascending order by volume
Mesh = Meshes sorted in ascending order by volume"""
__author__ = "sverremh"
__version__ = "2023.08.07"
# Import module to operate on Rhino geometry
import Rhino.Geometry as rg
# sort geometry by volume
geometry.sort(key = lambda el: rg.VolumeMassProperties.Compute(el).Volume)
# create a dispatch pattern
pattern = [isinstance(el, rg.Brep) for el in geometry] # list of True/False
# find the breps
breps = [el for el, test in zip(geometry, pattern) if test]
#find the meshes. Note that we use not keyword to check for false values
meshes = [el for el, test in zip(geometry, pattern) if not test]
# set output
Brep = breps
Mesh = meshes
Below is a simple example of how GH Python might be used to create a custom pattern of spheres in Grasshopper: