this post was submitted on 22 Nov 2023
294 points (100.0% liked)

196

16293 readers
37 users here now

Be sure to follow the rule before you head out.

Rule: You must post before you leave.

^other^ ^rules^

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] riodoro1@lemmy.world 23 points 10 months ago (1 children)

Python has great list comprehension. Too bad it’s incomprehensible to humans.

But seriously whoever writes those cool oneliners is a shitty programmer. Life is not code golf, fuck off with your smart ass.

[–] Bonsoir@lemmy.ca 13 points 10 months ago* (last edited 10 months ago)

What would be the alternative? (assuming that you want to do the loop yourself)

new_results = []
for result in results:
    if result:
        new_results.append(result)
results = new_results

or else

for result in results:
    if not result:
        results.remove(result)

which doesn't do the exact same thing.
Honestly, this list comprehension is much faster to read and quite easy to understand.
I think we could rename the "result" variable "x" or "res" and it would be less confusing though.