On Thu, May 7, 2020 at 4:42 AM Kirill Balunov <kirillbalunov@gmail.com> wrote:
`chain(*iterable)` converts iterable into a tuple, concretizing it in memory. chain.from_iterable(iterable) is lazy and goes through the elements one a time, meaning iterable can be infinite.

"meaning iterable can be infinite"  -  thank you, I missed this part, but to be honest I don’t remember when I do something useful with infinite iterables.

Being fuzzy about "infinite" versus "very large" here are a couple examples:

>>> def first_ints():
...     from random import randint
...     while True:
...         yield range(randint(10, 20))
...
>>> from itertools import chain
>>> nums = chain.from_iterable(first_ints())
>>> from itertools import islice
>>> list(islice(nums, 100, 150))
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 1, 2, 3, 4, 5, 6]

In this code we want initial sequences of non-negative integers of varying lengths, repeated... because MATH.  I could not concretize the infinite collection of finite sequences.

Here's an example that is just "very big" instead of infinite, but would still urge for .from_iterable().

>>> def all_man_lines(path='/usr/share/man/man1/'):
...     from glob import glob
...     for fname in glob(f"{path}*.gz"):
...         yield gzip.open(fname)
...
>>> lines = chain.from_iterable(all_man_lines())
>>> for line in islice(lines, 100, 105):
...     print(line)
...
b".    ds ^ \\\\k:\\h'-(\\\\n(.wu*10/11-\\*(#H)'^\\h'|\\\\n:u'\n"
b".    ds , \\\\k:\\h'-(\\\\n(.wu*8/10)',\\h'|\\\\n:u'\n"
b".    ds ~ \\\\k:\\h'-(\\\\n(.wu-\\*(#H-.1m)'~\\h'|\\\\n:u'\n"
b".    ds / \\\\k:\\h'-(\\\\n(.wu*8/10-\\*(#H)'\\z\\(sl\\h'|\\\\n:u'\n"
b'.\\}\n'

I do not have infinitely many man pages on my system, but I have enough of them that I don't want to open file handles to all of them at once.

--
The dead increasingly dominate and strangle both the living and the
not-yet born.  Vampiric capital and undead corporate persons abuse
the lives and control the thoughts of homo faber. Ideas, once born,
become abortifacients against new conceptions.