Insert item before each element of a list
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.
--Matt
--
http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
On Monday, October 8, 2012 10:06:50 PM UTC-4, Roy Smith wrote:
> In article ,
>
(big snip)
>
>
> > y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in
> > range(len(x
>
>
>
> A statement ending in four close parens is usually going to be pretty
>
> difficult to figure out. This is one where I had to pull out my pencil
>
> and start pairing them off manually to figure out how to parse it.
Fair enough. I admit I was looking for a tricky one-liner, which rarely leads
to good code...I should know better.
Thanks for all the feedback from everyone. It's amazing how much Python one
can learn just asking about a small section of code!
--
http://mail.python.org/mailman/listinfo/python-list
