For simple list creations, it is recommended to use list comprehension. However, when it comes to more complicated scenarios, spanning the list creation across multiple lines is more advisable.
e.g. for a simple task
[x for x in range(10)]
e.g. for a more complicated task
result = []
for x in complicated:
if y is complex:
for z in intricate_stuff:
result.append(convoluted(z))
(I probably would still use a multi-line list comprehension for the above example, but anyways you get the idea.)
Recently, I found myself achieve list creation like that using a generator like:
def gen():
for x in complicated:
if y is complex:
for z in intricate_stuff:
yield convoluted(z)
result = [x for x in gen()]
It's more natural for me to use a generator like this to create a list (well, generate a list), but I feel it might add unnecessary complexity.
Is this recommended? or, which way is more pythonic?