Combine lists


#1

Hi all, I’m trying to create a new list or replace items in a list based on values from another list, please see below. I want to take each item in List B and replace it only if the item in List A is true:

Combine listsI think one can achieve this through the List.Combine node but I don’t know how to build the Combinator for it. There is also the List.Replace node, but you cannot specify a condition using another list (you have to use the list wired in “item”). Any help would be greatly appreciated, thanks!

 

Dave


#2

Hi Dave,

It must be possible with original Dynamo nodes. However it’s much easier with Python.

이미지 1

It’s very convenient to use Python script especially when you want to deal with muliple lists at once.


#3

Thanks Hyunu, very much appreciated :slight_smile: I guess I’ll have to bite the bullet and learn some Python! I just couldn’t figure out a way to do this with regular nodes. I’m still learning Dynamo and list management is very important to do anything really. There are several list nodes that I’m just starting to use, especially those that use combinators, and other nodes that use keyProjector (those I’m having a hard time understanding without seeing an example since there’s not much when it comes to documentation).


#4

Dave your lists present a bit of a challenge because they’re lists containing sub-lists. The challenge stems from the fact that most solutions might require you to flatten your list even if you might want to preserve that list structure. Luckily Dynamo’s “List.FilterByBoolMask” is pretty powerful and can handle any list rank and one of its “features” is that it tries to preserve that list’s rank. Combine that with one of Andreas’s many useful nodes and you end up with a very robust solution.

Capture

 

I tried mimicking the clockwork node with only Dynamo’s default nodes, but it seems they’re not up to the challenge yet. The “List.ReplaceItemAtIndex” node can’t handle nested lists, hopefully someone will look into it. The clockwork node resorts to python, just like Kim suggested.


#5

Thanks again Dimitar. Do you mind reposting the image with a link to the full image? I cannot see what you did there! I’ll need to start collecting good resources to learn the Python syntax.


#6

Link fixed in the above post.


#7

Here is another definition to achieve your goal. Only original nodes are used. I don’t think using List.Combine node for this is a good idea. And here is a tip that most of lists which has one-item sublists are usually meaningless. I usually flatten that kinds of lists and use List.Chop at the end if necessary.

이미지 2

At the above definition, notice that you can’t get muliple indices by using IndexOf node. bottom is a threads about the issue.

http://dynamobim.com/forums/topic/indexof-cant-find-multiple-index-really/

You can use python if necessary. Yes, Python is a magic key in Dynamo. :slight_smile:

이미지-2a

 

 

 

 

 


#8

Another way of dealing with this.

01

Normally a DS Function solve this kind of issues in a very straightforward way.

def ReplaceList (ListA:var[],ListB:var[],Str)
{
ListB2=ListB[0]?Str:ListA[0];
return=ListB2;
};

Please notice that the function is expecting that every item of the list is a list by itself, like you showed in your sample.

 


#9

Just noticed that the function didn’t return the item as lists. Y little change made this:

def ReplaceList (ListA:var[],ListB:var[],Str)
{
ListB2=ListB[0]?Str:ListA[0];
return={ListB2};
};

02

 


#10

Thanks Hyunu. I wondered how I could get the indices from a list. This was very helpful, especally the link to the other post.

Thanks Eduardo. I haven’t learned any DS yet, need to go through the tutorial videos while they’re still up! And as Hyunu said, I realize that Python is definitely a must know. It’s not that bad actually. I think I got scared because since I don’t know the Revit API and thought it would be a hurdle, but at least to evaluate and build lists etc., it seems very manageable and powerful.


#11

Oh, it’s a nice simple operator, Eduardo. I see that DS operators are similar with C.

However, I found the syntax of the ternary operator in Python instead.

Opertion:return A when Bool is true, else return B

Design Script: Bool ? A : B

Python Script: A if Bool else B

Thanks, Eduardo.

이미지 1


#12

Thanks all again for your feedback. My final solution was through Python. I also ended up finding out the syntax for the ternary operator and used it as well to concatenate some string values.

Nested Ifs