On 9 October 2012 13:55, Peter Otten <span dir="ltr"><<a href="mailto:__peter__@web.de" target="_blank">__peter__@web.de</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>Duncan Booth wrote:<br>
<br>
> <a href="mailto:mooremathewl@gmail.com" target="_blank">mooremathewl@gmail.com</a> wrote:<br>
><br>
>> What's the best way to accomplish this? Am I over-complicating it?<br>
>> My gut feeling is there is a better way than the following:<br>
>><br>
>>>>> import itertools<br>
>>>>> x = [1, 2, 3]<br>
>>>>> y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in<br>
>>>>> range(len(x)))) y<br>
>> ['insertme', 1, 'insertme', 2, 'insertme', 3]<br>
>><br>
>> I appreciate any and all feedback.<br>
>><br>
><br>
> Given the myriad of proposed solutions, I'm surprised nobody has suggested<br>
> good old list slicing:<br>
<br>
</div>My post on gmane<br>
<br>
<a href="http://thread.gmane.org/gmane.comp.python.general/718940/focus=718947" target="_blank">http://thread.gmane.org/gmane.comp.python.general/718940/focus=718947</a><br>
<br>
apparently didn't make it through to the list.<br>
<div><br>
>>>> x = [1,2,3]<br>
>>>> y = ['insertme']*(2*len(x))<br>
>>>> y[1::2] = x<br>
>>>> y<br>
> ['insertme', 1, 'insertme', 2, 'insertme', 3]<br>
<br>
</div>An advantage of this approach -- it is usually much faster.<br></blockquote><div><br></div><div>It did, at least for me. People often don't see my posts too, I've been told. That said, sometimes people just skim threads so it may have been that.</div>
<div><br></div><div>On topic, the best methods are either:</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="courier new, monospace">inserts = itertools.repeat("insert") </font></blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="courier new, monospace">itertools.chain.from_iterable(zip(lst, inserts))</font></blockquote>
<div><br></div><div>or the more explicit:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<font face="courier new, monospace">def interleave(*lsts):</font></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<font face="courier new, monospace"> interleaved_in_tuples = zip(*lsts)</font></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<font face="courier new, monospace"> return itertools.chain.from_iterable(interleaved_in_tuples)</font></blockquote></div><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span style="font-family:'courier new',monospace">inserts = itertools.repeat("insert")</span><br><font face="courier new, monospace">interleave(inserts, lst)</font></blockquote><div><br></div><div>They explain what they want and they do it. Anyone who's interleaving without zip needs to re-read the built-ins. The manual two-yields is good, too, but it's much less flexible. If the speed benefits of slicing are important but the scaling is not, you really shouldn't be using CPython.</div>
</div>