On 8 October 2012 20:28, <span dir="ltr"><<a href="mailto:mooremathewl@gmail.com" target="_blank">mooremathewl@gmail.com</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">
What's the best way to accomplish this? Am I over-complicating it? 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 range(len(x))))<br>
>>> y<br>
['insertme', 1, 'insertme', 2, 'insertme', 3]<br>
<br>
I appreciate any and all feedback.<br></blockquote><div><br></div><div>That's probably not the best way.</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">list(itertools.chain.from_iterable(zip(itertools.repeat("insertme"), [1, 2, 3])))</font></blockquote><div>(tested)</div><div><br></div><div>written like this:</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("insertme")</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">itertools.chain.from_iterable(zip(inserts, x)) </blockquote>
<div>(untested)</div><div><br></div><div>But it's not far. I wouldn't use Ian Kelly's method (no offence), because of len(x): it's less compatible with iterables. Others have ninja'd me with good comments, too.</div>
</div>