On 8 October 2012 20:28, <mooremath...@gmail.com> wrote: > 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: > > >>> import itertools > >>> x = [1, 2, 3] > >>> y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in > range(len(x)))) > >>> y > ['insertme', 1, 'insertme', 2, 'insertme', 3] > > I appreciate any and all feedback. >
That's probably not the best way. list(itertools.chain.from_iterable(zip(itertools.repeat("insertme"), [1, 2, > 3]))) (tested) written like this: inserts = itertools.repeat("insertme") itertools.chain.from_iterable(zip(inserts, x)) (untested) 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.
-- http://mail.python.org/mailman/listinfo/python-list