<div dir="ltr"><div class="F0XO1GC-ed-a"><div tabindex="0" class="F0XO1GC-nb-P"><div style="overflow: auto;"><div style="max-height: 10000px;"><div dir="ltr">I thought that itertools.chain.from_iterable isn't useful.<div>cuz this only allow "single-nested iterable -- this will raise error when arg has non-nested element--" like below::</div><div><div style="border-width: 1px; border-style: solid; border-color: rgb(187, 187, 187); background-color: rgb(250, 250, 250); word-wrap: break-word;"><code><div>>>> from itertools import chain</div><div>>>> chain.from_iterable([1])</div><div><itertools.chain object at 0x00000112D332BF98></div><div>>>> list(_)</div><div>Traceback (most recent call last):</div><div>  File "<stdin>", line 1, in <module></div><div>TypeError: 'int' object is not iterable</div></code></div><br>and this can't unpack over double nest.<div style="border-width: 1px; border-style: solid; border-color: rgb(187, 187, 187); background-color: rgb(250, 250, 250); word-wrap: break-word;"><code><div>>>> chain.from_iterable([[[1, 2]], [[4, 5]]])</div><div><itertools.chain object at 0x00000112D3330B00></div><div>>>> list(_)</div><div>[[1, 2], [4, 5]]</div></code></div> </div><div>So, I wanted to make "True chain.from_iterable". and this is it.</div><div><div style="border-width: 1px; border-style: solid; border-color: rgb(187, 187, 187); background-color: rgb(250, 250, 250); word-wrap: break-word;"><code><pre style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-family: "MS ゴシック"; font-size: 9pt;"><pre style="font-family: "MS ゴシック"; font-size: 9pt;"><span style="color: rgb(0, 0, 128); font-weight: bold;">def </span>flatten(iterables, unpack=(<span style="color: rgb(0, 0, 128);">list</span>, <span style="color: rgb(0, 0, 128);">tuple</span>, <span style="color: rgb(0, 0, 128);">set</span>), peep=(<span style="color: rgb(0, 0, 128);">list</span>, <span style="color: rgb(0, 0, 128);">tuple</span>, <span style="color: rgb(0, 0, 128);">set</span>)):<br>    <span style="color: rgb(0, 0, 128); font-weight: bold;">for </span>element <span style="color: rgb(0, 0, 128); font-weight: bold;">in </span>iterables:<br>        <span style="color: rgb(0, 0, 128); font-weight: bold;">try</span>:<br>            <span style="color: rgb(0, 0, 128); font-weight: bold;">if </span><span style="color: rgb(0, 0, 128);">isinstance</span>(element, unpack):<br>                <span style="color: rgb(0, 0, 128); font-weight: bold;">if </span><span style="color: rgb(0, 0, 128);">isinstance</span>(element, peep):<br>                    <span style="color: rgb(0, 0, 128); font-weight: bold;">yield from </span>flatten(element, <span style="color: rgb(102, 0, 153);">unpack</span>=unpack, <span style="color: rgb(102, 0, 153);">peep</span>=peep)<br>                <span style="color: rgb(0, 0, 128); font-weight: bold;">else</span>:<br>                    <span style="color: rgb(0, 0, 128); font-weight: bold;">yield from </span>flatten(element, <span style="color: rgb(102, 0, 153);">unpack</span>=(), <span style="color: rgb(102, 0, 153);">peep</span>=())<br>            <span style="color: rgb(0, 0, 128); font-weight: bold;">elif </span><span style="color: rgb(0, 0, 128);">isinstance</span>(element, peep):<br>                <span style="color: rgb(0, 0, 128); font-weight: bold;">yield </span><span style="color: rgb(0, 0, 128);">type</span>(element)(flatten(element, <span style="color: rgb(102, 0, 153);">unpack</span>=unpack, <span style="color: rgb(102, 0, 153);">peep</span>=peep))<br>            <span style="color: rgb(0, 0, 128); font-weight: bold;">else</span>:<br>                <span style="color: rgb(0, 0, 128); font-weight: bold;">raise </span><span style="color: rgb(0, 0, 128);">TypeError<br></span><span style="color: rgb(0, 0, 128);">        </span><span style="color: rgb(0, 0, 128); font-weight: bold;">except </span><span style="color: rgb(0, 0, 128);">TypeError</span>:<br>            <span style="color: rgb(0, 0, 128); font-weight: bold;">yield </span>element</pre></pre></code></div><div><br></div>Reason why I didin't use type() is wanted to unpack type-object like "range"</div><div>and I wanted to unpack user-defined-class/func. this is why I didin't use collections.Iterable to check instance.<br><br></div><div>I know this will be destructed by itertools.count :(</div><div><br></div><div>Please give me advice.</div><div><br></div><div>And I wanna know why function like this is not in standard library.</div><div><br></div><div>thx.</div></div></div></div></div><div></div><div></div></div><div><div class="F0XO1GC-ed-a"></div></div><div class="F0XO1GC-nb-b"><div class="F0XO1GC-nb-a F0XO1GC-nb-cb"><div style="display: inline-block;"></div><div class="F0XO1GC-md-a"></div></div></div></div>