Insert item before each element of a list

mooremathewl at gmail.com mooremathewl at gmail.com
Tue Oct 9 10:03:54 EDT 2012


On Monday, October 8, 2012 10:06:50 PM UTC-4, Roy Smith wrote:
> In article <mailman.1976.1349747963.27098.python-list at python.org>,
> 

(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!



More information about the Python-list mailing list