Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #30968 > unrolled thread
| Started by | mooremathewl@gmail.com |
|---|---|
| First post | 2012-10-08 12:28 -0700 |
| Last post | 2012-10-09 14:55 +0200 |
| Articles | 20 on this page of 24 — 16 participants |
Back to article view | Back to comp.lang.python
Insert item before each element of a list mooremathewl@gmail.com - 2012-10-08 12:28 -0700
Re: Insert item before each element of a list Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-08 13:42 -0600
Re: Insert item before each element of a list MRAB <python@mrabarnett.plus.com> - 2012-10-08 20:43 +0100
Re: Insert item before each element of a list Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-08 14:04 -0600
Re: Insert item before each element of a list Agon Hajdari <agonh@freenet.de> - 2012-10-08 22:12 +0200
Re: Insert item before each element of a list Peter Otten <__peter__@web.de> - 2012-10-08 23:12 +0200
RE: Insert item before each element of a list "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-08 21:15 +0000
Re: Insert item before each element of a list Agon Hajdari <agonh@freenet.de> - 2012-10-08 23:39 +0200
RE: Insert item before each element of a list "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-08 22:12 +0000
Re: Insert item before each element of a list Paul Rubin <no.email@nospam.invalid> - 2012-10-08 15:24 -0700
Re: Insert item before each element of a list Nobody <nobody@nowhere.com> - 2012-10-08 23:35 +0100
Re: Insert item before each element of a list "Alex" <foo@email.invalid> - 2012-10-09 00:08 +0000
Re: Insert item before each element of a list Terry Reedy <tjreedy@udel.edu> - 2012-10-08 21:58 -0400
Re: Insert item before each element of a list Roy Smith <roy@panix.com> - 2012-10-08 22:06 -0400
Re: Insert item before each element of a list rusi <rustompmody@gmail.com> - 2012-10-08 19:34 -0700
Re: Insert item before each element of a list rusi <rustompmody@gmail.com> - 2012-10-08 19:39 -0700
Re: Insert item before each element of a list Hans Mulder <hansmu@xs4all.nl> - 2012-10-12 00:21 +0200
Re: Insert item before each element of a list Terry Reedy <tjreedy@udel.edu> - 2012-10-11 19:38 -0400
Re: Insert item before each element of a list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-12 02:16 +0000
Re: Insert item before each element of a list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-09 12:01 +0000
Re: Insert item before each element of a list alex23 <wuwei23@gmail.com> - 2012-10-08 20:11 -0700
Re: Insert item before each element of a list mooremathewl@gmail.com - 2012-10-09 07:03 -0700
Re: Insert item before each element of a list Duncan Booth <duncan.booth@invalid.invalid> - 2012-10-09 11:40 +0000
Re: Insert item before each element of a list Peter Otten <__peter__@web.de> - 2012-10-09 14:55 +0200
Page 1 of 2 [1] 2 Next page →
| From | mooremathewl@gmail.com |
|---|---|
| Date | 2012-10-08 12:28 -0700 |
| Subject | Insert item before each element of a list |
| Message-ID | <fc55bb2e-fdc0-405c-89b5-290aa4fc6c63@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-10-08 13:42 -0600 |
| Message-ID | <mailman.1961.1349725360.27098.python-list@python.org> |
| In reply to | #30968 |
On Mon, Oct 8, 2012 at 1:28 PM, <mooremathewl@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', 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))
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2012-10-08 20:43 +0100 |
| Message-ID | <mailman.1962.1349725395.27098.python-list@python.org> |
| In reply to | #30968 |
On 2012-10-08 20:28, mooremathewl@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', 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))
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-10-08 14:04 -0600 |
| Message-ID | <mailman.1965.1349726710.27098.python-list@python.org> |
| In reply to | #30968 |
On Mon, Oct 8, 2012 at 1:52 PM, Joshua Landau <joshua.landau.ws@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 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.
[toc] | [prev] | [next] | [standalone]
| From | Agon Hajdari <agonh@freenet.de> |
|---|---|
| Date | 2012-10-08 22:12 +0200 |
| Message-ID | <mailman.1966.1349727261.27098.python-list@python.org> |
| In reply to | #30968 |
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]
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-10-08 23:12 +0200 |
| Message-ID | <mailman.1967.1349730731.27098.python-list@python.org> |
| In reply to | #30968 |
mooremathewl@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', 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]
[toc] | [prev] | [next] | [standalone]
| From | "Prasad, Ramit" <ramit.prasad@jpmorgan.com> |
|---|---|
| Date | 2012-10-08 21:15 +0000 |
| Message-ID | <mailman.1968.1349732214.27098.python-list@python.org> |
| In reply to | #30968 |
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 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.
[toc] | [prev] | [next] | [standalone]
| From | Agon Hajdari <agonh@freenet.de> |
|---|---|
| Date | 2012-10-08 23:39 +0200 |
| Message-ID | <mailman.1969.1349732492.27098.python-list@python.org> |
| In reply to | #30968 |
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 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)]
[toc] | [prev] | [next] | [standalone]
| From | "Prasad, Ramit" <ramit.prasad@jpmorgan.com> |
|---|---|
| Date | 2012-10-08 22:12 +0000 |
| Message-ID | <mailman.1971.1349734374.27098.python-list@python.org> |
| In reply to | #30968 |
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 for item 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
map instead.
>>> y = [('insertme', i) for i in x]
>>> z = []
>>> _ = map( z.extend, y )
>>> z
['insertme', 0, 'insertme', 1, 'insertme', 2, 'insertme', 3, 'insertme', 4]
I am not 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.
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2012-10-08 15:24 -0700 |
| Message-ID | <7x1uh8y53n.fsf@ruckus.brouhaha.com> |
| In reply to | #30968 |
mooremathewl@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)))
================
etc.
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2012-10-08 23:35 +0100 |
| Message-ID | <pan.2012.10.08.22.35.56.530000@nowhere.com> |
| In reply to | #30968 |
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]
[toc] | [prev] | [next] | [standalone]
| From | "Alex" <foo@email.invalid> |
|---|---|
| Date | 2012-10-09 00:08 +0000 |
| Message-ID | <k4vpt2$n2s$1@dont-email.me> |
| In reply to | #30968 |
mooremathewl@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', 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."
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-10-08 21:58 -0400 |
| Message-ID | <mailman.1976.1349747963.27098.python-list@python.org> |
| In reply to | #30968 |
On 10/8/2012 3:28 PM, mooremathewl@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', 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
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-10-08 22:06 -0400 |
| Message-ID | <roy-323E36.22065008102012@news.panix.com> |
| In reply to | #30983 |
In article <mailman.1976.1349747963.27098.python-list@python.org>,
Terry Reedy <tjreedy@udel.edu> wrote:
> On 10/8/2012 3:28 PM, mooremathewl@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', 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.
[toc] | [prev] | [next] | [standalone]
| From | rusi <rustompmody@gmail.com> |
|---|---|
| Date | 2012-10-08 19:34 -0700 |
| Message-ID | <d0a5ccce-4a18-41e2-b804-795891db895b@lb2g2000pbc.googlegroups.com> |
| In reply to | #30984 |
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 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]
[toc] | [prev] | [next] | [standalone]
| From | rusi <rustompmody@gmail.com> |
|---|---|
| Date | 2012-10-08 19:39 -0700 |
| Message-ID | <4ebfe84f-dfaa-40cb-88d9-414660d0c42f@ro10g2000pbc.googlegroups.com> |
| In reply to | #30986 |
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 x)) ['insert', 1, 'insert', 2, 'insert', 3]
[toc] | [prev] | [next] | [standalone]
| From | Hans Mulder <hansmu@xs4all.nl> |
|---|---|
| Date | 2012-10-12 00:21 +0200 |
| Message-ID | <50774685$0$6943$e4fe514c@news2.news.xs4all.nl> |
| In reply to | #30987 |
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: > >>>> reduce(operator.add, (['insert', a] for a in x)) > ['insert', 1, 'insert', 2, 'insert', 3] Or, if you don't want to import the operator module: sum((['insert', a] for a in x), []) -- HansM
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-10-11 19:38 -0400 |
| Message-ID | <mailman.2074.1349998762.27098.python-list@python.org> |
| In reply to | #31136 |
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 different parens on the other side:
>>
>>>>> reduce(operator.add, (['insert', a] for a in x))
>> ['insert', 1, 'insert', 2, 'insert', 3]
>
> Or, if you don't want to import the operator module:
>
> sum((['insert', a] for a in x), [])
All of the solutions based on adding (concatenating) lists create an
unneeded temporary list for each addition except the last and run in
O(n**2) time. Starting with one list and appending or extending (which
does two appends here) is the 'proper' approach to get an O(N) algorithm.
This does not matter for n=3, but for n = 10000 it would.
expanded = []
expand = expand.append
for item in source:
expand('insert')
expand(item)
is hard to beat for clarity and time.
expanded = []
expand = expand.extend
for item in source:
expand(['insert', item])
might be faster if creating the list is faster than the second expand
call. Note that a typical lisp-like version would recursively traverse
source to nil and build expanded from tail to head by using the
equivalent of
return ['insert' item].extend(expanded)
Extend would be O(1) here also since it would at worst scan the new list
of length 2 for each of the items in the source.
def interleave(source):
for item in source:
yield 'insert'
yield item
list(interleave(source))
might also be faster since it avoids the repeated python level call. I
prefer it anyway as modern, idiomatic python in that it separates
interleaving from creating a list. In many situations, creating a list
from the interleaved stream will not be needed.
--
Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-10-12 02:16 +0000 |
| Message-ID | <50777d74$0$6574$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #31136 |
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 prefers the different parens on the other side: >> >>>>> reduce(operator.add, (['insert', a] for a in x)) >> ['insert', 1, 'insert', 2, 'insert', 3] > > Or, if you don't want to import the operator module: > > sum((['insert', a] for a in x), []) Which is also O(N**2) like the reduce solution above. That means that it will seem perfectly fine when you test it using a a hundred or so items, then some day you'll pass it a list with ten million items and it will take 36 hours to complete. I'm serious by the way. By my tests, increasing the number of items in the list by a factor of ten increases the time taken by between 30 and 300 times. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-10-09 12:01 +0000 |
| Message-ID | <5074120b$0$6574$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #30986 |
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
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web