i have an iterator that yields a list each time it is iterated. i want to get all of its lists concatenated together as one big long list. easy enough, but, i want to do it in a comprehension and am coming up empty trying to code that.
from itertools import chain
result = list(chain.from_iterable(generate_the_lists()))
i was trying to come up with some way to construct it but i was unable to. this suggests there isn't such a simple way. i would have need to make a function ... or use this.
You just do a double list comprehension. The trick to these is to do the for clauses 'backwards' to what you might think:
Output:
>>> data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> [item for row in data for item in row]
[1, 2, 3, 4, 5, 6, 7, 8, 9]