If we only use Dynamo to run the programs we write on a single element at a time, Dynamo's strength in automating repetitive tasks is put to waste. This video will cover the basics of creating and managing lists to make sure that our scripts are capable of carrying out the same task on several inputs, not just one.
- [Voiceover] It's fairly common, when we're using Dynamo, to be faced with lists of information. The first few times that you see information organized this way, it can seem a little bit daunting. But, in fact, you've probably already worked with lists of data, and you might not even know it. While you're working in Revit, or AutoCAD, or any other CAD software, if you've ever selected more than one object or element, and changed something about them all at the same time, maybe moved them to a different location, or deleted them, or changed one of their properties, then you already have some experience working with lists. Of course, working with Dynamo lists is a little bit different from selecting multiple objects in Revit or AutoCAD.
Understanding how to manage information that's organized in this way is a very important hurdle to get over if you're going to get Dynamo to do powerful work for you. Let's go over some of the most common techniques to manage data that's arranged in lists. Here we have two lists, one of which we've assembled just by inputting three values, a 10, a zero,and a two. And we also have a number sequence node, which is generating a sequence of numbers, starting with zero, and counting by two until we've reached 10 items. Let's begin this example by dropping in a "get item at index" node, which we'll find under the Core List section of the Node Library.
When data is arranged in a list, each item has a number in square brackets called its index. The index is kind of like the address, or the identifier, for each piece of data in a list. This node will help you get a specific piece of data from a list by its index. Basically, you use this node to point to any list and say, "I need the second item in this list." Or, the first, or the third. Whatever you specify in the index input. We'll also need to drop in a number slider in order to specify that index. I'll do a quick search for "number" using the white search box at the top of our Node library.
Maybe I'm interested in getting the data that lives in index 3, coming out of the sequence output. I'll plug this "sequence" node into our list input, and we'll specify "index 3" as the index input. As you can see, our node outputs to the number six. Let's head back over to the "sequence" node and look up what data lives at index 3. And, in fact, it's the number six. If we were to change the number input to, say, nine, it should output number 18 instead.
Let's give that a try. And you can see that Dynamo automatically recalculates, and we're pulling the number 18 now, because it lives at the index 9, coming out of the "sequence" node. Now, let's try another helpful list-related node, called "add item to front." "Add item to front," as its name suggests, will add an item of data that we specify to the beginning, or the front, of an existing list. I'm interested in plugging the output of our "sequence" node into the list input of our new "add item to front" node.
We also need to specify the new item that we'd like to add to the beginning of this list. Since the sequence list begins at zero and counts up by two, let's add a negative two to the beginning of the list to keep with that pattern. I'll copy-paste a number input from above using Control+C and Control+V to paste. And I'll change the number 9 to a -2. Then, I'll plug it into our item input.
Checking out the node preview, you can see that our list now begins with -2, which is the item that we added, and then it's followed by the rest of the list that came out of the "sequence" node. Now, you might also notice that, in the data that's coming out of the "sequence" node, the number zero had an index, again, that's the number in square brackets, of zero. But, if we look at the data coming out of our "add item to front," the zero now has an index of one. That's because the index of every list you'll ever work with in Dynamo will always begin with zero, and it will always increase by one for each item in the list.
Data doesn't necessarily keep its index from one node to the next. Now, let's take a look at some other helpful nodes for working with lists. You might notice that right next to the "AddItemToFront" node in the Node Library, is "AddItemToEnd." Which, you might expect, will work in the same way, but instead of adding an item to the beginning of a list, it will add the item to the end. Let's drop in a "shuffle" node, which is going to randomize the order of the elements of the list that we provide. I'm going to pull down this group of nodes in order to keep our workspace tidy.
And, again, I'll plug in the output of the "sequence" node into the input of our list shuffle. Looking at the node preview, it looks like the data is rearranged randomly. This can be a useful node if you're a designer that's interested in using Dynamo to generate an element of randomness, maybe on your building's facade, or for a feature element in your project. Now, let's drop in another fairly straightforward node, called "Count," which is going to return the total number of the items in your list.
"Count" also takes only a single input. And when we check out the node output, we can see that there are ten items in this list. Now, in another video, we used the "ListCreate" node to manually build up a list of elements. We told Dynamo how many items we wanted in our list, and then we provided a single number input for each one. Here was our result, the 10, a zero, and a two. Now, let's see what happens when we use this same "ListCreate" node, but, instead of feeding it single numbers for inputs, we plug in a list of items.
I'm going to select the "ListCreate" node that's already on our workspace. And, again, I'll use Control+C to copy it, and Control+V to paste a new instance. In order to keep our workspace clean, I'm again going to click and drag our group of nodes. You might notice that, in the node that we just copied, Dynamo remembered to keep the three number sliders plugged in as inputs. We're not interested in using them as inputs anymore, so let's go ahead and disconnect them. I'm going to click on each input, and then click off in space to disconnect the wires.
For this example, let's plug in the data coming out of the "sequence" node as item zero, or the first item in our new list. And then, let's use the "List Create" as our second item. We're not interested in having a third item, so I'm going to go ahead and click the minus button to remove that item, too. Now, let's take a look at what comes out on the other end. Looking at our node preview, we seem to have a little bit more going on here than any of the other examples that we've seen so far. You can see that I can collapse this sub-list by clicking on the triangle icon next to each list header.
This might make a little bit more sense now, right? We only plugged in two items as inputs, which are the two lists that we hooked up. Let's maximize the first item by again clicking on the triangle. Here we see the contents of the nested list, or the sub-list, which exactly matches the data that's coming out of the "sequence" node. Zero, two, four, down through 18. In the same way, if we maximize the list at index 1, we see the same exact data that's coming out of the original "List Create" node. 10, zero, two.
So, in short, a list is allowed to be an item within another list, and plenty of Dynamo nodes and workflows actually require data to be structured in this way in order to perform properly. Now that we've covered a few nodes that are useful for managing lists of data, let's look at how we manage nested lists of data in the next video.
- Placing and connecting Dynamo nodes
- Understanding Dynamo's data types
- Performing math functions
- Creating number lists and text strings
- Writing data to an Excel spreadsheet
- Creating points, curves, surfaces, and solids
- Analyzing geometry
- Linking a Dynamo-driven SAT into Revit
- Placing Revit families with Dynamo
- Creating Revit views and sheets with Dynamo