Python idiom to chain (flatten) an infinite iterable of finite iterables? [duplicate]
Starting with Python 2.6, you can use itertools.chain.from_iterable
:
itertools.chain.from_iterable(iterables)
You can also do this with a nested generator comprehension:
def flatten(iterables):
return (elem for iterable in iterables for elem in iterable)
Use a generator:
(item for it in infinite for item in it)
The * construct unpacks into a tuple in order to pass the arguments, so there's no way to use it.