Velocity Reviews > Insert item before each element of a list

# Insert item before each element of a list

mooremathewl@gmail.com
Guest
Posts: n/a

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

Ian Kelly
Guest
Posts: n/a

 10-08-2012
On Mon, Oct 8, 2012 at 1: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]
>
> I appreciate any and all feedback.

Using the "roundrobin" recipe from the itertools documentation:

x = [1, 2, 3]
y = list(roundrobin(itertools.repeat('insertme', len(x)), x))

MRAB
Guest
Posts: n/a

 10-08-2012
On 2012-10-08 20:28, 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.
>

Slightly better is:

y = list(itertools.chain.from_iterable(('insertme', i) for i in x))

Ian Kelly
Guest
Posts: n/a

 10-08-2012
On Mon, Oct 8, 2012 at 1:52 PM, Joshua Landau
<(E-Mail Removed)> 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

That's fair, I probably wouldn't use it either. It points to a
possible need for a roundrobin variant that truncates like zip when
one of the iterables runs out. It would have to do some look-ahead,
but it would remove the need for the len(x) restriction on
itertools.repeat.

Agon Hajdari
Guest
Posts: n/a

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

Peter Otten
Guest
Posts: n/a

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

Less general than chain.from_iterable(izip(repeat("insertme"), x)):

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

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

Guest
Posts: n/a

 10-08-2012
Agon Hajdari wrote:

> Sent: Monday, October 08, 2012 3:12 PM
> To: (E-Mail Removed)
> 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 that? It seems to work just fine for me.

>>> x

[0, 1, 2, 3, 4]

>>> [('insertme', i) for i in x]

[('insertme', 0), ('insertme', 1), ('insertme', 2), ('insertme', 3), ('insertme', 4)]

>
> y = [item for tup in y for item in tup]
>

This email is confidentialand subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.

Agon Hajdari
Guest
Posts: n/a

 10-08-2012
On 10/08/2012 11:15 PM, Prasad, Ramit wrote:
> Agon Hajdari wrote:
>> Sent: Monday, October 08, 2012 3:12 PM
>> To: (E-Mail Removed)
>> 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 that? It seems to work just fine for me.
>
>>>> x

> [0, 1, 2, 3, 4]
>>>> [('insertme', i) for i in x]

> [('insertme', 0), ('insertme', 1), ('insertme', 2), ('insertme', 3), ('insertme', 4)]
>
>>
>> y = [item for tup in y for item in tup]
>>

>
> This email is confidential and subject to important disclaimers and
> conditions including on offers for the purchase or sale of
> securities, accuracy and completeness of information, viruses,
> confidentiality, legal privilege, and legal entity disclaimers,
> available at http://www.jpmorgan.com/pages/disclosures/email.
>

I think he wanted to have a 'plain' list
a = [0, 1, 0, 2, 0, 3]
and not
a = [(0, 1), (0, 2), (0, 3)]

Guest
Posts: n/a

 10-08-2012
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, 2, 3, 4]

> >>>> [('insertme', i) for i in x]

> > [('insertme', 0), ('insertme', 1), ('insertme', 2), ('insertme', 3),('insertme', 4)]
> >

> >>
> >> y = [item for tup in y foritem in tup]

>
> I think he wanted to have a 'plain' list
> a = [0, 1, 0, 2, 0, 3]
> and not
> a = [(0, 1), (0, 2), (0,3)]

You are absolutely correct. I missed that when I tried it.
Instead of the nested list comprehension, I might have used

>>> y = [('insertme', i) for i in x]
>>> z = []
>>> _ = map( z.extend, y )
>>> z

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

I amnot sure which is more Pythonic, but to me map + list.extend tells
me more explicitly that I am dealing with an iterable of iterables.
It might make more sense to only to me though.

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.

Paul Rubin
Guest
Posts: n/a

 10-08-2012
(E-Mail Removed) 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)))

================

etc.