<div>On Mon, Oct 8, 2012 at 12:28 PM, <span dir="ltr"><<a href="mailto:mooremathewl@gmail.com" target="_blank">mooremathewl@gmail.com</a>></span> wrote:</div><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></blockquote><div><br></div><div>There is no need to use range and iterate over the indices:</div><div>list(itertools.chain.from_iterable(('insertme', i) for i in x))</div>
<div><br></div><div>You could simplify that even farther to a simple list expression:</div><div>[('insertme', i) for i in x]</div><div><br></div><div><div>You could also use zip and repeat:</div><div>zip(itertools.repeat('insertme'), x) # Use itertools.izip for a generator rather than a list.</div>
</div><div><br></div><div>All code is untested.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
I appreciate any and all feedback.<br>
<br>
--Matt<br>
<span class="HOEnZb"><font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br>