Listcomprehensions, where?

Hello Dynos,

I just filter strings by length. Where can i optimize my script!
I want just remove “0” strings…

# Phython-Unterstützung aktivieren und DesignScript-Bibliothek laden
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

num = IN[0]
c = []
OUT = []

for i in IN[0]:
    c.append(len(i))

for x in c:
    if x <= 0:
	    OUT.append(True)
    else:
	    OUT.append(False)


KR
Andreas

Hi @Draxl_Andreas,


OUT = filter(None,IN[0])

2 Likes

@Draxl_Andreas I would go with what @Alban_de_Chasteigner has suggested.
But since you wanted to know List Comprehensions, I would have done it this way.
image

OUT = [True if len(x) <=0 else False for x in IN[0]]

I can’t think of a scenario where the length would be ever be less than 0 so you can just say len(x) == 0 instead!

5 Likes