Oops...

This website is made for modern browsers. You are seeing this, because your browser is missing a feature or two. Please install the latest update or switch to a modern browser. See you soon.

Filter lists

#lists

The methods presented below can be used to extract or remove items or subsets from lists. The components operate on given indices or patterns of Boolean values. The how-to Search lists deals with a similar operation: searching lists for specific items.

Retrieve items per index (List Item)

The component List Itemcan be used to fetch specific items from a list L by a given index i. If input W (Wrap) is set to True, the index enumeration of the list will repeat from the beginning, in case that the index exceeds the number of items in the list.

Sometimes, we only need the first and last index of a list. We can zoom in on a List Itemcomponent and add an extra output at the top via the ZUI. By default, input i is set to 0, the new output is then -1 and thus gives us the last item of the list.

Separate lists (Split List)

The component Split Listwill separate a list L in two parts. Index i states the item that will be the first one on the second list.

Subdivide lists (Partition List)

The component Partition Listwill subdivide a list L into subsets. Their length is given by input S.

Extract a subset (Sub List)

The component Sub Listwill extract a subset from a List L, according to the domain provided at input D.

Remove items per index (Cull Index)

The component Cull Indexwill remove items from a List L, according to the indices given at input I. This component resembles the opposite of List Item.

Remove every nth item (Cull Nth)

The component Cull Nthwill remove every nth item from a list L. The frequency is given by input N.

Remove random items (Random Reduce)

The component Random Reducewill randomly remove a given number of items R from a list L. Simultaneously, the original order of the items is scrambled. We can influence the randomness by providing an alternative Seed S.

Remove items by using a mask (Cull Pattern)

The component Cull Patternwill remove items from a list L, according to a pattern P of Boolean values. If the number of items in the list exceeds the length of the pattern, the pattern is repeated until all items are processed. By right-clicking input P and selecting Invert, the pattern can be inverted (False will become True etc.).

Get the complement of a set

To get the complement of a set, we need the set itself and the universal set ($A^C = U\backslash A$). If the sets consist of or can be converted into Primitive Data Types, we can use the component Set Differenceto get the complement. Otherwise, we can get the indices of our subset with Item Indexand use Cull Indexto remove the subset from our universal set to get the complement.

Two possibilities to get the complement of a set.

If we have the indices, but they occur multiple times, we can use Create Seton the whole list of indices to get unique values at output S. We then use List Itemand Cull Indexto generate the two complementary lists.

Finding the complement set after merging indices.

Dispatch items into two target lists (Dispatch)

The component Dispatchwill send the items of list L to one of the two target lists, according to the pattern of Boolean values provided at input P. If the number of items exceeds the number of Boolean values, the pattern is repeated. We get all items, whose corresponding Boolean was True, at output A and vice versa at for output B.

Before we can use Dispatch, we have to create the list of Boolean values, which is often the more effortful task. The components found in the ribbon tab Math in group Operators can be used for simple logical comparisons. The components Evaluateand Expressioncan be used for more advanced operators. Also, components with analysis functionality, like Point In Brepor Is Planar, will output a list of Boolean values. You will also find more examples of generating Booleans in the how-to conditional-statements.

Dispatch items into multiple target lists (e.g. filter by type)

As written above, Dispatchhas only two target lists (one for True and one for False). However, by combining some components, we can write a filter that handles any type of filtering. This is done by checking each criterion and then dispatching the matches to separate branches of a data tree.

The following example shows how to filter a list of geometric objects into separate lists for each type. The idea is to test each item with string comparison, whether it matches one of our criteria. But before we can do this, we have to cast all objects into string format, which is done by a Textcontainer. We can then use Expressionand the notation Contains(x,y) to test, if a string contains any of the strings that we provide in a Panelat the other input. Points are not cast by their type name but by their coordinates. Here we test, if the character { is present, to identify a point. To ensure that every object is matched with every string, we have to graft input y.

After the custom evaluation, we can filter the list with a Dispatchcomponent. The positive matches for each type are in separate branches and we can attach an Explode Treecomponent at output A to find our separated lists.

The task of filtering a list by type can also be achieved with a C# Scriptcomponent. How to code this is explained here.

Separate items into multiple lists by an integer pattern (Sift pattern)

The component Sift Patternis similar to Dispatch but its pattern P is not made with Booleans (0 and 1) but with integers. The items at input L will be passed to the output that corresponds with the provided integer. The ZUI can be used to add outputs. Instead of just forwarding the items exclusively to the corresponding list, their index at the other lists is filled with null items.

Remove null items

The simplest way to remove all null items from a list, is to use Clean Tree. See Clean Treefor more information. Input N (Remove Nulls) is set to True by default.

Another possibility is to use Null Itemto find the index of any null item. We can then use Cull Pattern, on which we Invert input P, to remove all positive matches from a list.

Group items that share equal properties

For bundling items that share an equal property, we find the components for this operation in the Grasshopper ribbon tab Sets.

For example, if we want to group lines by their length, we can calculate their lengths and use Create Setto compute how many unique values there are. We will then connect output S to a Member Indexcomponent, which will retrieve the indices for the items that belong to each unique value. A List Itemcomponent will then create separate lists for each property.

Another way to receive a similar result is to use output M of Create Set, which returns to which new index the original item was remapped. We can then use Sift Patternto generate lists that share the same property each. The generated lists will have null items.

This page is open source. Edit it on GitHub or see how you can contribute.

Up next