I’m trying to rotate a vertical line around 360 degrees using API, my code works as shown in the image below, but I’m looking to enhance it by by redefining the CreateTransformed method using list comprehension in a single line…a task I’m currently struggling with!?
Just work backwards. You have all the variables and methods defined individually, so just start at the last line and begin replacing the inline variable with its definition from the previous line, one by one. Also, what’s the reason for using list comprehension if you’ve already got working code that is easy to follow?
Just for brevity and to avoid writing lengthy code.
I tried this code, and it works as shown in the image below, however, I don’t know if it’s the optimized one.
out_V_rebar = [out_vert_bar.CreateTransformed(Transform.CreateRotation(XYZ.BasisZ, n*out_V_angle)) for n in range(out_V_rebar_count+1)]
In my opinion, list comprehension is actually less “efficient” here as you now have a whole bunch of stuff defined and called in a single line. It is technically shorter, but it’s harder to read and understand on the fly.
As for optimized, you can’t get any more optimized than list comprehension in terms of comparable code length. You’re also not doing anything computationally heavy here, so optimization isn’t a huge concern compared to clarity.