3 ms·
I find chain much easier to read than nested comprehensions: from itertools import chain list(chain(*trees)) list(chain.from_iterable(trees)) l
by js2 4y ago
I find chain much easier to read than nested comprehensions:
from itertools import chain
list(chain(*trees))
list(chain.from_iterable(trees))
list(chain(*dog_breeds.values()))
list(chain.from_iterable(dog_breeds.values()))
- Jensson 4y agoNested comprehensions are a more powerful construct, you can do more with it than chain or the itertools module. Chain is cleaner for this case since it is weaker, so it is clearer what it does, but since list comprehension is more powerful you ought to learn it first and chain is just a nicety.
- js2 4y agoI’ve been using Python as my primary language since before it had list comprehensions and I still find nested comprehensions to be hard to comprehend, hard to compose, and hard to refactor. I usually resort to building up generator expressions, using itertools, or writing traditional loops. $0.02.