Building on the concepts covered in the previous video, this video addresses some of the challenges of working with nested lists, or lists that contain lists as its items. First we'll cover how a nested list is structured, and then we will adapt some of the same techniques from the previous video to run successfully in a nested list.
- [Voiceover] In a another video, we imported data from an Excel spreadsheet, which came into Dynamo as a nested list. Now that we have a better understanding of how nested lists are constructed, let's dig into a few of the nodes that'll help us manage the information that's structured this way. In a previous example, we pointed to an Excel file that was on our computer, and we read the data that was available in the table. As we can see in the node preview for list Rest of Items, what we have is a nested list, nine items long, each consisting of five subitems.
Let's begin with the Get Item At Index node, which we used previously to pick a specific piece of data from a list by its index. We'll plug in the Rest Of Items node as our list, and we'll also need to specify a number, which will be the index of data we're interested in pulling. Let's drop in a Number node, and set the value to one, which should help us to return the data at Index One of this list.
Looking now at the node preview, it looks like we've extracted a list of data, rather than a single value. Let's compare our output to the output from the Rest Of Items node. Looking at Index One, beginning with S2 then Shirts, then 36, 63, it looks like the information that we've extracted with the Get Item At Index actually matches perfectly, as to Shirts, 36, 63. So, in the case of nested lists, when you pick an item using Get Item At Index, you'll extract multiple pieces of data, if there's a list that lives at that particular index.
Now on to one of the most confusing nodes to understand in Dynamo, but also one of the most powerful nodes in the Library: List Map. List Map is used to force a node to work on a different level of the list. In other words, List Map allows us to specify a function that we want to perform on every item of a list. For example, we just used Get Item At Index to pick out Index One of our parent list, and the output was a list of five items. But what if we aren't interested in looking at the data for a shelf type S2? What if, instead, we meant to see every shelf type's description, or Index One, of every sublist? We need to drill down one more level to the sublist level.
That's where List Map comes in. Let's try it out as an example. First, we need to specify what list we want to act on. So, again, let's plug in our Rest Of Item as the parent list. Next, we have to define a function. Conceptually, this is the tricky part, because, so far in Dynamo, we've always seen data flow from left to right. It's about to feel like we're forcing data to flow backwards. The function that we want to perform here is to get Index One of every item of the parent list. That's accomplished by these two nodes right here, Get Item At Index and the Number One.
So let's copy the Index One nodes from above and we'll hook them into F of X, or Function. We're going to use Control + C to copy and Control + V on my keyboard to paste. Now that we've copied our Index One function, let's hook them up into F of X. Now our List Map turns yellow, which means we have an error. The reason here isn't intuitive at all, but this is a crucial step to getting List Map to work. Let's take a quick step back. Do you notice anything funny about the way that we're connecting these nodes? Again, this isn't too obvious, but notice that we're plugging the parent list into two different locations inside the same workflow.
First we plug it into the List Map node, as our parent list, but we're also plugging that same exact data into part of the List Map's function input. Why should List Map need the same exact data twice? The answer here is that, any time we use List Map, we need to leave one single input within our function, or these two nodes, blank. Remember, the point of List Map is that we're going to take a list and we're going to perform this function on every item within that list. The input that we leave blank is our way of telling Dynamo exactly which input in our function we would want to run on each item of the parent list.
In our case, we want to run each item of the parent list through the list input of Get Item At Index. So, let's remove this input and leave it blank to see how Dynamo responds. It looks like our error is clear. Let's see what's coming out of the List Map node now. Excellent, it looks like it's doing exactly what we set out to accomplish. This is Index One of every item within our nested list. Here we can see Shirts, Shirts, Pants, Pants, Footwear. In the same way, Shirts, Shirts, Pants, Pants, Footwear, and so on and so forth.
Nice work, you now have the List Map workflow under your belt. Let's repeat the List Map workflow again, but this time, instead of using Get Item At Index, we'll try the Add Item To Front node, which we've also used a few minutes ago on another single list. I'm going to go ahead and copy the List Map node again, using Control + C and Control + V to paste. We can continue using our parent list as our list input, but instead, we'd like to use the Add Item To Front as our input for the function.
If you remember from the last time that we used Add Item To Front, we need to specify an item that we'd like to put at the beginning of a list. Let's use a String node this time. Maybe we'll add the text First New Item, so we can easily identify it. Now comes the question of which input we want to plug our string into. If you remember from before, we need to leave one of these two inputs empty if the List Map is going to function properly.
Let's plug our string input into the Item input of the Add Item To Front. Again, there might be some temptation to plug in our nested list from above into the list input of Add Item To Front, but remember, we need to leave one node empty as a way of indicating which list we'd like to perform this action on for every single item inside the list. Let's take a look at the data coming out of our List Map node. Once again, it seems that our List Map node is working. Each sublist now begins with the item First New Item, followed by the rest of the list that was there already.
Let's finish with one more node that's really powerful for working with nested lists, called Transpose. If you're familiar with the transpose feature in Excel, this is the equivalent in Dynamo. Transpose is also found under the Core list portion of our Node Library. In order to clean up our data a little bit, I'm going to move our group of nodes down closer to the Transpose node that we just placed on the canvas. You'll notice that the Transpose node only takes one input, called lists.
Let's plug in Rest Of Items into lists, and let's see what comes out the other side. Now, you might remember that, when we first imported this data from Excel into Dynamo, we identified that all of the data in the same Excel row is grouped together as the sublists in Dynamo, and the data that was within the same Excel column all share the same index within their respective sublists. Transpose takes our nested lists, and it flips that organization so that column data is now grouped together, and the row data now shares the same index.
To say that differently, we started with a list that was nine items long, and each item had a sublist that contained five items. But if we look at what's coming out of the Transpose node, we now have a list of five items total, each sublist totaling nine items long. The data itself hasn't changed. The only thing that's different is how the data is organized. Now we have the sama data grouped together by the shelf type's properties, rather than grouped together by shelf type. So we've now covered some of the basics of creating and managing lists, to make sure that our scripts are capable of carrying out the same task on several inputs and not just one.
If we only use Dynamo to run the programs that we write on a single element at at time, the strength that Dynamo has in processing lots of information and automating repetitive tasks is wasted.
- 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