Velocity Reviews > Insert item before each element of a list

# Insert item before each element of a list

Nobody
Guest
Posts: n/a

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

['insertme', 1, 'insertme', 2, 'insertme', 3]

Alex
Guest
Posts: n/a

 10-09-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) 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', 1, 'insertme', 2, 'insertme', 3]
>
> I appreciate any and all feedback.
>
> --Matt

Just like the Zen of Python (http://www.python.org/dev/peps/pep-0020/)
says . . . "There should be at least ten-- and preferably more --clever
and obscure ways to do it."

Terry Reedy
Guest
Posts: n/a

 10-09-2012
On 10/8/2012 3:28 PM, (E-Mail Removed) 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', 1, 'insertme', 2, 'insertme', 3]

The straightforward, crystal-clear, old-fashioned way

>>> lst = []
>>> for item in [1,2,3]:

lst.append('insert me')
lst.append(item)

>>> lst

['insert me', 1, 'insert me', 2, 'insert me', 3]

Paul Rubin's list(gfunc(prefix, lst)) is similar in execution.

--
Terry Jan Reedy

Roy Smith
Guest
Posts: n/a

 10-09-2012
In article <(E-Mail Removed)>,
Terry Reedy <(E-Mail Removed)> wrote:

> On 10/8/2012 3:28 PM, (E-Mail Removed) 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', 1, 'insertme', 2, 'insertme', 3]

>
> The straightforward, crystal-clear, old-fashioned way
>
> >>> lst = []
> >>> for item in [1,2,3]:

> lst.append('insert me')
> lst.append(item)

I'm going to go with this one. I think people tend to over-abuse list
comprehensions. They're a great shorthand for many of the most common
use cases, but once you stray from the simple examples, you quickly end
up with something totally obscure.

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

rusi
Guest
Posts: n/a

 10-09-2012
On Oct 9, 7:06*am, Roy Smith <(E-Mail Removed)> wrote:
> In article <(E-Mail Removed)>,
> *Terry Reedy <(E-Mail Removed)> wrote:
>
>
>
>
>
>
>
>
>
> > On 10/8/2012 3:28 PM, (E-Mail Removed) 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', 1, 'insertme', 2, 'insertme', 3]

>
> > The straightforward, crystal-clear, old-fashioned way

>
> > *>>> lst = []
> > *>>> for item in [1,2,3]:
> > * *lst.append('insert me')
> > * *lst.append(item)

>
> I'm going to go with this one. *I think people tend to over-abuse list
> comprehensions. *They're a great shorthand for many of the most common
> use cases, but once you stray from the simple examples, you quickly end
> up with something totally obscure.
>
> > 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.

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]

rusi
Guest
Posts: n/a

 10-09-2012
On Oct 9, 7:34*am, rusi <(E-Mail Removed)> 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]

alex23
Guest
Posts: n/a

 10-09-2012
On Oct 9, 12:06*pm, Roy Smith <(E-Mail Removed)> 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.

Steven D'Aprano
Guest
Posts: n/a

 10-09-2012
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 going to be frequently interleaving sequences, a helper
function is a good solution. Here's my muxer:

def mux(*iterables):
"""Muxer which yields items interleaved from each iterator or
sequence argument, stopping when the first one is exhausted.

>>> list( mux([1,2,3], range(10, 15), "ABCD") )

[1, 10, 'A', 2, 11, 'B', 3, 12, 'C']
"""
for i in itertools.izip(*iterables): # in Python 3 use builtin zip
for item in i:
yield item

Then call it like this:

py> list(mux(itertools.repeat("insert me"), range(5)))
['insert me', 0, 'insert me', 1, 'insert me', 2, 'insert me', 3, 'insert
me', 4]

--
Steven

Peter Otten
Guest
Posts: n/a

 10-09-2012
Duncan Booth wrote:

> (E-Mail Removed) 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', 1, 'insertme', 2, 'insertme', 3]
>>
>> I appreciate any and all feedback.
>>

>
> Given the myriad of proposed solutions, I'm surprised nobody has suggested
> good old list slicing:

My post on gmane

apparently didn't make it through to the list.

>>>> x = [1,2,3]
>>>> y = ['insertme']*(2*len(x))
>>>> y[1::2] = x
>>>> y

> ['insertme', 1, 'insertme', 2, 'insertme', 3]

An advantage of this approach -- it is usually much faster.

mooremathewl@gmail.com
Guest
Posts: n/a

 10-09-2012
On Monday, October 8, 2012 10:06:50 PM UTC-4, Roy Smith wrote:
> In article <(E-Mail Removed)>,
>

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