Re: Insert item before each element of a list

2012-10-12 Thread Joshua Landau
On 9 October 2012 13:55, Peter Otten __pete...@web.de wrote: Duncan Booth wrote: mooremath...@gmail.com wrote: 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 =

Re: Insert item before each element of a list

2012-10-11 Thread Hans Mulder
On 9/10/12 04:39:28, rusi wrote: On Oct 9, 7:34 am, rusi rustompm...@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:

Re: Insert item before each element of a list

2012-10-11 Thread Terry Reedy
On 10/11/2012 6:21 PM, Hans Mulder wrote: On 9/10/12 04:39:28, rusi wrote: On Oct 9, 7:34 am, rusi rustompm...@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

Re: Insert item before each element of a list

2012-10-11 Thread Steven D'Aprano
On Fri, 12 Oct 2012 00:21:57 +0200, Hans Mulder wrote: On 9/10/12 04:39:28, rusi wrote: On Oct 9, 7:34 am, rusi rustompm...@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

Re: Insert item before each element of a list

2012-10-09 Thread Duncan Booth
mooremath...@gmail.com wrote: 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',

Re: Insert item before each element of a list

2012-10-09 Thread Steven D'Aprano
On Mon, 08 Oct 2012 19:34:26 -0700, rusi 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] That works, but all those list additions are going to be slow. It will be an O(N**2) algorithm. If you're

Re: Insert item before each element of a list

2012-10-09 Thread Peter Otten
Duncan Booth wrote: mooremath...@gmail.com wrote: 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

Re: Insert item before each element of a list

2012-10-09 Thread mooremathewl
On Monday, October 8, 2012 10:06:50 PM UTC-4, Roy Smith wrote: In article mailman.1976.1349747963.27098.python-l...@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

Re: Insert item before each element of a list

2012-10-08 Thread Ian Kelly
On Mon, Oct 8, 2012 at 1:28 PM, mooremath...@gmail.com wrote: 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

Re: Insert item before each element of a list

2012-10-08 Thread Chris Kaynor
On Mon, Oct 8, 2012 at 12:28 PM, mooremath...@gmail.com wrote: 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

Re: Insert item before each element of a list

2012-10-08 Thread MRAB
On 2012-10-08 20:28, mooremath...@gmail.com wrote: 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

Re: Insert item before each element of a list

2012-10-08 Thread Joshua Landau
On 8 October 2012 20:28, mooremath...@gmail.com wrote: 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

Re: Insert item before each element of a list

2012-10-08 Thread Ian Kelly
On Mon, Oct 8, 2012 at 1:52 PM, Joshua Landau joshua.landau...@gmail.com wrote: But it's not far. I wouldn't use Ian Kelly's method (no offence), because of len(x): it's less compatible with iterables. Others have ninja'd me with good comments, too. That's fair, I probably wouldn't use it

Re: Insert item before each element of a list

2012-10-08 Thread Agon Hajdari
On 10/08/2012 09:45 PM, Chris Kaynor wrote: [('insertme', i) for i in x] This is not enough, you have to merge it afterwards. y = [item for tup in y for item in tup] -- http://mail.python.org/mailman/listinfo/python-list

Re: Insert item before each element of a list

2012-10-08 Thread Peter Otten
mooremath...@gmail.com wrote: 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',

RE: Insert item before each element of a list

2012-10-08 Thread Prasad, Ramit
Agon Hajdari wrote: Sent: Monday, October 08, 2012 3:12 PM To: python-list@python.org Subject: Re: Insert item before each element of a list On 10/08/2012 09:45 PM, Chris Kaynor wrote: [('insertme', i) for i in x] This is not enough, you have to merge it afterwards. Why do you say

Re: Insert item before each element of a list

2012-10-08 Thread Agon Hajdari
On 10/08/2012 11:15 PM, Prasad, Ramit wrote: Agon Hajdari wrote: Sent: Monday, October 08, 2012 3:12 PM To: python-list@python.org Subject: Re: Insert item before each element of a list On 10/08/2012 09:45 PM, Chris Kaynor wrote: [('insertme', i) for i in x] This is not enough, you have

RE: Insert item before each element of a list

2012-10-08 Thread Prasad, Ramit
Agon Hajdari wrote: On 10/08/2012 11:15 PM, Prasad, Ramit wrote: Agon Hajdari wrote: On 10/08/2012 09:45 PM, Chris Kaynor wrote: [('insertme', i) for i in x] This is not enough, you have to merge it afterwards. Why do you say that? It seems to work just fine for me. x [0, 1,

Re: Insert item before each element of a list

2012-10-08 Thread Paul Rubin
mooremath...@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)))

Re: Insert item before each element of a list

2012-10-08 Thread Nobody
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)]

Re: Insert item before each element of a list

2012-10-08 Thread Alex
mooremath...@gmail.com wrote: 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

Re: Insert item before each element of a list

2012-10-08 Thread Terry Reedy
On 10/8/2012 3:28 PM, mooremath...@gmail.com wrote: 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

Re: Insert item before each element of a list

2012-10-08 Thread Roy Smith
In article mailman.1976.1349747963.27098.python-l...@python.org, Terry Reedy tjre...@udel.edu wrote: On 10/8/2012 3:28 PM, mooremath...@gmail.com wrote: 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:

Re: Insert item before each element of a list

2012-10-08 Thread rusi
On Oct 9, 7:06 am, Roy Smith r...@panix.com wrote: In article mailman.1976.1349747963.27098.python-l...@python.org,  Terry Reedy tjre...@udel.edu wrote: On 10/8/2012 3:28 PM, mooremath...@gmail.com wrote: What's the best way to accomplish this?  Am I over-complicating it?  My

Re: Insert item before each element of a list

2012-10-08 Thread rusi
On Oct 9, 7:34 am, rusi rustompm...@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

Re: Insert item before each element of a list

2012-10-08 Thread alex23
On Oct 9, 12:06 pm, Roy Smith r...@panix.com wrote: I'm going to go with this one.  I think people tend to over-abuse list comprehensions. I weep whenever I find `_ = [...]` in other people's code. -- http://mail.python.org/mailman/listinfo/python-list