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

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

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

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

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

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]

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

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))

Re: Insert item before each element of a list

2012-10-08 Thread alex23
On Oct 9, 12:06 pm, Roy Smith 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

Re: Insert item before each element of a list

2012-10-08 Thread rusi
On Oct 9, 7:34 am, 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] Or if one prefers the different parens on the other side: >>> reduce(operator.add, (['insert', a] for a in x)) ['i

Re: Insert item before each element of a list

2012-10-08 Thread rusi
On Oct 9, 7:06 am, Roy Smith wrote: > In article , >  Terry Reedy 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 Roy Smith
In article , Terry Reedy 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: > > > import itertools > x = [1, 2, 3] > y = list(itertoo

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 range(len(x)))

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

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 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'

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

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: >>> [(&#

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 hav

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)))

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 Ian Kelly
On Mon, Oct 8, 2012 at 1:52 PM, Joshua Landau 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 either. It points to a poss

Re: Insert item before each element of a list

2012-10-08 Thread Joshua Landau
On 8 October 2012 20:28, 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)

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 range(len(x

Re: Insert item before each element of a list

2012-10-08 Thread Chris Kaynor
On Mon, Oct 8, 2012 at 12:28 PM, 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 > rang

Re: Insert item before each element of a list

2012-10-08 Thread Ian Kelly
On Mon, Oct 8, 2012 at 1:28 PM, 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 ran