Insert item before each element of a list

Nobody nobody at nowhere.com
Mon Oct 8 18:35:57 EDT 2012


On Mon, 08 Oct 2012 12:28:43 -0700, mooremathewl wrote:

>>>> 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 for j in [1,2,3] for i in ('insertme', j)]
	['insertme', 1, 'insertme', 2, 'insertme', 3]




More information about the Python-list mailing list