Optimize the use of IF in a list

Good afternoon everyone, I am trying to optimize a set of nodes where I start with a list that I have to re-validate with a value, if correct it will have the value AA if different it retains the value of the original list, the level of difficulty is in I have to validate the result of the list, replace the value and convert it into a new list with the previous and new values, this process is more than 100 times, can someone help me how to optimize it.

Thank you all.

1 = AA
2 = BB
3 = CC
4 = DD

Build a dictionary with all the standard mappings (note: keys have to be strings so you’ll need to convert any numbers). Use the dictionary to convert any standard key values to their appropriate alternate. Anything that isn’t in the dictionary will return a null - that’s ok. You can then compare the new list (with nulls) to the original list with a conditional to return the original value for any null and the existing value for anything else.


The problem is that there are about 200 comparisons.


Hi Nick, but since the dictionary is generated, since the originating list will never have null values.

Original :

Hola Nick, pero como se genera el diccionario, ya que la lista origina nunca tendrá valores nulos.

Hi @Rafael_Padilla could you please translate to english in the future, as i have done here with Google…, the forum is in english, thanks :slight_smile:

If you have the dictionary already generated then I’d use Nick’s method.
If you’re going through the alphabet think about maybe using Python so you don’t have to write out every mapping… Something like:
chr(64 + i) * 2 for i in range(1, 27)

No need for python here. Ranges in Dynamo are quite effective for this and have a lower entry point as you don’t need to have the knowledge of what chr(64) returns. :slight_smile:

Each to their own. Personally I find that far more confusing.

That was assuming that you’d have non-standard values that would be left unchanged at the end. If you don’t, then a dictionary is all you need. You haven’t given us enough information to know exactly what your scenario is so we can only suggest relative solutions for this type of problem. The point is that you have to define the mapping somewhere. A dictionary is the most flexible option and gives you easy 1-to-1 conversions.

Hello everyone, I appreciate your views and of course I will provide more information Nick,

I get a list from modeled elements (List RVT), I have to compare it with a value (Condition 1) and if a value is true it will be replaced by the value of condition 1, keeping the false values, creating a new list (List TO). This new list will be compared again with condition 2 and if true it will be replaced by the value of condition 2, preserving the false values, generating a new list (List B) and so on until all values ​​are compared and replaced, It is something that is repeated 200 times, there are no null values ​​because everything is compared and replaced.

This comparison is extensive and consists of many nodes, hence the need to optimize it or change the procedure to something more efficient or compact.

Definitely the case for a dictionary. Define the dictionary with the original values and the new values and then get the new values from your list. That’s about as optimized as you can be.

I was able to do the theory exercise as described and it was very useful, however, when I put it into practice I did not achieve the result.

Theoretical exercise

Practical exercise

What am I doing wrong?

A dictionary key needs to be a unique value. You are feeding many keys for seven values.
Before converting to strings, flatten, reduce by using a unique node and sort. Then have the same number of values as keys.

I would create the dictionary from two paired lists so the substitutions are known

Alternately you can do it with list indexing if are OK with a node with an indexing error

Thanks Mike.Buttery, I solved it this way, however it seems to me that there are things that can be optimized further.

Final practical exercise

What do you think, is this the maximum that can be optimized?

Mike, I think your solution is the best optimized. you can avoid warnings by using List.AllIndicesOf rather than List.IndexOf

1 Like

Gracias a todos por sus comentarios.
estupenda tarde!

Updated using List.Contains for the test and List.AllIndicesOf


What do you mean by “optimized”?

You’re repeating functions for different inputs which isn’t optimized but does make your graph easier to follow. Is your graph slow to run? Do you need to increase execution speed? Are you worried about scaling this logic for other use cases? “Optimized” can mean a lot of things here and not every one of them is important.

The most “optimized” logic you could create for this is to simply write out every key, value pair you have. That’s two lines of a single code block and two dictionary nodes for the whole thing, but it doesn’t look optimized because it’s manual.

A lot of the “inefficiencies” I’m seeing are from a lack of list handling. That’s very common with beginners and not a big deal because you can typically work around it with extra nodes. If that’s what you’re wanting to optimize then your focus should be on understanding data structures, list levels, and how to manipulate and maintain lists. It really has nothing to do with the specific logic of this graph.

Also, DesignScript can go a long way to simplify your nodes and error handling. Using the IF shorthand in a code block allows you to execute two functions separately and only when neccessary, allowing you to avoid many null or empty warnings.


Good afternoon Nick,
You are right, the concept of optimized can have different meanings depending on the context, what I mean is having a graph that contains the necessary elements, runs as quickly as possible, that the weight of the file is light and, above all, that does not have user intervention.
Regarding the DesingScript and repeating nodes, I am aware that they can be omitted but the doubt in the graph could not be clear, for this reason I go to the forum for a solution because I clarify, the solution prior to the post was developed and worked correctly but with the shortcomings that you comment (slow execution speed and a very large graph).