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

后端 未结 3 970
感情败类
感情败类 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:43

    The former can only handle unpackable iterables. The latter can handle iterables that cannot be fully unpacked, such as infinite generators.

    Consider

    >>> from itertools import chain
    >>> def inf():
    ...     i=0
    ...     while True:
    ...         i += 1
    ...         yield (i, i)
    ... 
    >>> x=inf()
    >>> y=chain.from_iterable(x)
    >>> z=chain(*x)
    
    

    Furthermore, just the act of unpacking is an eager, up-front-cost activity, so if your iterable has effects you want to evaluate lazily, from_iterable is your best option.

提交回复
热议问题