Insert item before each element of a list

Paul Rubin no.email at nospam.invalid
Mon Oct 8 18:24:44 EDT 2012


mooremathewl at gmail.com writes:
>>>> x = [1, 2, 3] ..
>>>> y
> ['insertme', 1, 'insertme', 2, 'insertme', 3]

def ix(prefix, x):
   for a in x:
      yield prefix
      yield a

y = list(ix('insertme', x))

================

from itertools import *
y = list(chain.from_iterable(izip(repeat('insertme'), x)))

================

etc.



More information about the Python-list mailing list