Max is a life enhancer for tech & entrepreneurship. Which seeks to blend both to build innovative products or services for the world that solves hard problems.
List comprehension is one of my favorite tools for writing effective statements and fewer lines, but it needs to be done right. The example is likely slightly exaggerted (the
"fruit for fruit in fruits" bit), but I've seen it done. I believe they are more readable and easier to understand if the names of variables/elements are chosen properly — but we know that naming is the hardest thing :)
A long and complex list comprehension definitely suffers from being too "smart" and thus ignores readability.
However, when it comes to a short for loop, i.e. generating a list of numbers from a range, no one should be using a regular for loop with append .
# This is way more readable
nums=[iforiinrange(10)]# This is less readable, IMO we shouldn't have to do this
nums=[]foriinrange(10):nums.append(i)
Throwing in a single conditional if (i.e. the grapes example in the post) is fine IMHO. It really depends on what you and your team agree on at the end of the day. Nothing beats a good consistency in a shared codebase.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Tbh I don't really use list compensation because it suffers from being too clever but ignores readability and debugging.
Take for example this that you wrote a as good example.
What if you were to encounter a bug in this code.
How would you debug it? Would you spend more time figuring out what went wrong by looking at the logic or debug it step by step?
But because you put it as one liner it become difficult to understand or debug in a step by step process.
List comprehension is one of my favorite tools for writing effective statements and fewer lines, but it needs to be done right. The example is likely slightly exaggerted (the
"fruit for fruit in fruits" bit), but I've seen it done. I believe they are more readable and easier to understand if the names of variables/elements are chosen properly — but we know that naming is the hardest thing :)
A long and complex list comprehension definitely suffers from being too "smart" and thus ignores readability.
However, when it comes to a short for loop, i.e. generating a list of numbers from a
range
, no one should be using a regularfor
loop withappend
.Throwing in a single conditional
if
(i.e. thegrapes
example in the post) is fine IMHO. It really depends on what you and your team agree on at the end of the day. Nothing beats a good consistency in a shared codebase.