Difference between chain(*iterable) vs chain.from_iterable(iterable)

后端 未结 3 978
感情败类
感情败类 2020-12-20 13:17

I have been really fascinated by all the interesting iterators in itertools, but one confusion I have had is the difference between these two functions and why

3条回答
  •  余生分开走
    2020-12-20 13:41

    chain(*foo(5)) unpacks the whole generator, packs it into a tuple and processes it then.

    chain.from_iterable(foo(5)) queries the generator created from foo(5) value for value.

    Try foo(1000000) and watch the memory usage go up and up.

提交回复
热议问题