If true add a string at then end of item

Hi all!

This is my first post about Python as I am starting my learning path through this programming language!

I could not see any learning resource on the internet which is focused on Python sessions applied in Dynamo so I will have to look into many websites in order to learn the basics and then apply them into Dynamo environment.

Here I post a screenshot with the issue I am behind.

As you can see, I would like to add at the end of the input list of strings IN[0] the word ‘_CONTROL’ if the IN[1] input is true.

Thank you so much.

@JMCiller ,

actually you are doing this already… …where do you want to store the value?

I would like to get just one list with the proper values as I already did with Dynamo nodes:

image

Hi @JMCiller ,

See if something like this works for you:

values = IN[0]
conditions = IN[1]

OUT = []
i = 0

while i < len(values):
    if conditions[i]:
        OUT.append(values[i]+"_CONTROL")
    else:
        OUT.append(values[i])
    i = i + 1

PS: I am by no means any good in Python, if anyone has some suggestions to improve and/or simplify this, please don’t hesitate :slight_smile:

@JMCiller the statement you are using in your conditional if is a string, you should use a valid boolean(True in Python, true in DS), plus to make it work properly you couple the inputs of the lists items with the bools items using a zip function;
The second approach is a comprehension list, worth looking at if you are planning to learn Python:

5 Likes

Very nice! Thanks for sharing! I even got it to work with only 1 line based of your approach :smiley:

OUT = [x + "_CONTROL" if y == True else x for x,y in zip(IN[0],IN[1])]
2 Likes