Dynamo: Remove sublists with ContainsKey

Imagine you want to use Dynamo to place an adaptive component, let’s take an adaptive component with 2 insertion points. In that case you need to provide the AdaptiveComponent.ByPoints node a list with points organized by 2 in sublists:

chopped-list

List.Chop is a node that can be used to do so.
But what if the list has an impair number of points. In that case you will have at the end a sublist  with only one point:

impair

Of course the AdaptiveComponent.ByPoints node will return an error because it needs 2 points. This means that we need to check if the last list has just one point, if yes we need to remove this sublist.

There are several ways to do so. Here a first(but not the best) solution:

getridoflastlist1

What we do here is to count all the items in each sublist with the List.Count node. Then, with the == node,  we check if there is a sublist with just one item. With the IndexOf node we will get the indices of all these lists. And finally we remove them with the List.RemoveAtIndex node.

But as always in Dynamo you can have several solutions for the same problem. Here is a second solution using 3 nodes instead of 6:

getridoflastlist2

With the ContainsKey node we will check if all sublists have an item with index 1. Each sublist with 2 points will return true, but the sublists with just one point will return false.

With the FilterByBoolMask we can then filter out only the sublists whith the true value (=with 2 points in it).

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s