When creating lists, lists are used, for example:
squares = [item ** 2 for item in some_list]
For loops, it's better to do something with list items (or other objects):
for item in some_list: print(item)
Using comprehension for its side effects or for-loop to create a list is usually underestimated.
Some of the other answers here advocate turning understanding into a cycle when it gets too long. I don't think good style: the append calls needed to create a list are still ugly. Instead, reorganize into a function:
def polynomial(x): return x ** 4 + 7 * x ** 3 - 2 * x ** 2 + 3 * x - 4 result = [polynomial(x) for x in some_list]
Only if you are concerned about speed - and you have done your profiling! - You must keep a long, unreadable understanding of the list.
source share