Some features of Python are in nearly every guide, but are they are useful in practice? Consider the list comprehension syntax. It does a similar thing to the map
built-in in most programs—it creates a new list
from an existing iterable.
To be fair, list comprehension has some advantages over the map
built-in. It can remove elements with an if
-clause at the end. And it returns a list
, not an iterable—with map
, we would need to further call the list
built-in to get a list
.
But list comprehension can be worth avoiding because:
for
-loops and the map
built-in, can accomplish the same sorts of tasks.In my view, list comprehension is useful for creating lists with straightforward logic—like a list
of 100 positive integers. But for complex things, or in cases where a function call is needed, other approaches like for
-loops or map
are a better choice. It is good to keep list comprehensions, should you decide to use them, fairly short and simple.