Insert item before each element of a list

rusi rustompmody at gmail.com
Mon Oct 8 22:39:28 EDT 2012


On Oct 9, 7:34 am, rusi <rustompm... at gmail.com> wrote:
> How about a 2-paren version?
>
> >>> x = [1,2,3]
> >>> reduce(operator.add,  [['insert', a] for a in x])
>
> ['insert', 1, 'insert', 2, 'insert', 3]

Or if one prefers the different parens on the other side:

>>> reduce(operator.add, (['insert', a] for a in x))
['insert', 1, 'insert', 2, 'insert', 3]



More information about the Python-list mailing list