Python code for boolean list output


just starting out with python, so total noobism right here.
I have written a piece of code, and it works, but I have the feeling that it could be done better.

If you take a look at the way I am appending boolResults of statement evaluations… I am doing it to create an ouput list of boolean values. I have the feeling this is not the cleanest way of doing this.

Can anyone give a suggestion how to improve this?

PS one more question. What is the best way to share a piece of code on this forum? How can i neatly attach the code to my post? 2018-10-02%20(1)

Thanks in advance.

Do you really need python?

What are you trying to achieve with it?

yes, I would like to do it in Python, to learn.

I am trying to achieve a boolean list to use for a mask filter.

The elements to filter are in the form af A01-A A01-B B01-A C01-C etc.

I am practicing to write code that produces boolean results of true for last characters A and first characters A, D, E, H.

Hope i answered your question.

Bool in python would be True or False with capital letters :slight_smile:

Since you are dealing with strings, you could always use the str.startswith And str.endswith methods or if stringA in StringB or get last character and test if that character is in a list of characters. These all return a true or false if the conditions are met. There are quite a few ways to do what you want in truth, I would say play around and see what you like best.

And to attach code, you can use the </> button in the text window menu. Paste your code, select it, hit the </> button and it formats it correctly.

You are making a test ( if w[-1]=='C' and {w[0]}.issubset(MatchList) ), and depending on the value of the condition, you are creating a variable, and then appending the variable to outList. You should directly append the value of the test :slight_smile:

for w in NameList:
    outList.append(w[-1]=='C' and {w[0]}.issubset(MatchList))

I think this would be the easiest/clearest way to do it.


I would probably write it something like this, assuming you want “true/false” values…

import clr
NmLst = IN[0]
MatchLst = ('A','D','E','H')
outLst = []
for w in NmLst:
	if w[-1]=='C' and w[0] in MatchLst:
OUT = outLst

Otherwise I’d write it like so

But proposal from @mellouze works quite nicely as well:
Though I would change it to:

outLst.append(w[-1]=='C' and w[0] in MatchLst)


Thanks a lot, especially to you @mellouze and @Jonathan.Olesen. These were the kind of answers i was looking for :slight_smile: