Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Insert item before each element of a list
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  24 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
mooremath...@gmail.com  
View profile  
 More options Oct 8 2012, 3:28 pm
Newsgroups: comp.lang.python
From: mooremath...@gmail.com
Date: Mon, 8 Oct 2012 12:28:43 -0700 (PDT)
Local: Mon, Oct 8 2012 3:28 pm
Subject: Insert item before each element of a list
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Kelly  
View profile  
 More options Oct 8 2012, 3:43 pm
Newsgroups: comp.lang.python
From: Ian Kelly <ian.g.ke...@gmail.com>
Date: Mon, 8 Oct 2012 13:42:08 -0600
Local: Mon, Oct 8 2012 3:42 pm
Subject: Re: Insert item before each element of a list

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MRAB  
View profile  
 More options Oct 8 2012, 3:43 pm
Newsgroups: comp.lang.python
From: MRAB <pyt...@mrabarnett.plus.com>
Date: Mon, 08 Oct 2012 20:43:12 +0100
Local: Mon, Oct 8 2012 3:43 pm
Subject: Re: Insert item before each element of a list
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))))
>>>> 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))


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Kelly  
View profile  
 More options Oct 8 2012, 4:05 pm
Newsgroups: comp.lang.python
From: Ian Kelly <ian.g.ke...@gmail.com>
Date: Mon, 8 Oct 2012 14:04:38 -0600
Local: Mon, Oct 8 2012 4:04 pm
Subject: Re: Insert item before each element of a list
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 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.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Agon Hajdari  
View profile  
 More options Oct 8 2012, 4:14 pm
Newsgroups: comp.lang.python
From: Agon Hajdari <ag...@freenet.de>
Date: Mon, 08 Oct 2012 22:12:09 +0200
Local: Mon, Oct 8 2012 4:12 pm
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.

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Oct 8 2012, 5:12 pm
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Mon, 08 Oct 2012 23:12:07 +0200
Local: Mon, Oct 8 2012 5:12 pm
Subject: Re: Insert item before each element of a list

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]

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]

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Prasad, Ramit  
View profile  
 More options Oct 8 2012, 5:36 pm
Newsgroups: comp.lang.python
From: "Prasad, Ramit" <ramit.pra...@jpmorgan.com>
Date: Mon, 8 Oct 2012 21:15:27 +0000
Local: Mon, Oct 8 2012 5:15 pm
Subject: RE: Insert item before each element of a list

Agon Hajdari wrote:
> Sent: Monday, October 08, 2012 3:12 PM
> To: python-l...@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.  

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Agon Hajdari  
View profile  
 More options Oct 8 2012, 5:41 pm
Newsgroups: comp.lang.python
From: Agon Hajdari <ag...@freenet.de>
Date: Mon, 08 Oct 2012 23:39:26 +0200
Local: Mon, Oct 8 2012 5:39 pm
Subject: Re: Insert item before each element of a list
On 10/08/2012 11:15 PM, Prasad, Ramit wrote:

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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Prasad, Ramit  
View profile  
 More options Oct 8 2012, 6:13 pm
Newsgroups: comp.lang.python
From: "Prasad, Ramit" <ramit.pra...@jpmorgan.com>
Date: Mon, 8 Oct 2012 22:12:38 +0000
Local: Mon, Oct 8 2012 6:12 pm
Subject: RE: Insert item before each element of a list

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.  


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Rubin  
View profile  
 More options Oct 8 2012, 6:24 pm
Newsgroups: comp.lang.python
From: Paul Rubin <no.em...@nospam.invalid>
Date: Mon, 08 Oct 2012 15:24:44 -0700
Local: Mon, Oct 8 2012 6:24 pm
Subject: Re: Insert item before each element of a list

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

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

etc.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nobody  
View profile  
 More options Oct 8 2012, 6:35 pm
Newsgroups: comp.lang.python
From: Nobody <nob...@nowhere.com>
Date: Mon, 08 Oct 2012 23:35:57 +0100
Local: Mon, Oct 8 2012 6:35 pm
Subject: Re: Insert item before each element of a list

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]

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex  
View profile  
 More options Oct 8 2012, 8:08 pm
Newsgroups: comp.lang.python
From: "Alex" <f...@email.invalid>
Date: Tue, 9 Oct 2012 00:08:02 +0000 (UTC)
Local: Mon, Oct 8 2012 8:08 pm
Subject: Re: Insert item before each element of a list

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]

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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Oct 8 2012, 9:59 pm
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Mon, 08 Oct 2012 21:58:54 -0400
Local: Mon, Oct 8 2012 9:58 pm
Subject: Re: Insert item before each element of a list
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)

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

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

--
Terry Jan Reedy


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roy Smith  
View profile  
 More options Oct 8 2012, 10:06 pm
Newsgroups: comp.lang.python
From: Roy Smith <r...@panix.com>
Date: Mon, 08 Oct 2012 22:06:50 -0400
Local: Mon, Oct 8 2012 10:06 pm
Subject: Re: Insert item before each element of a list
In article <mailman.1976.1349747963.27098.python-l...@python.org>,
 Terry Reedy <tjre...@udel.edu> wrote:

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.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
rusi  
View profile  
 More options Oct 8 2012, 10:34 pm
Newsgroups: comp.lang.python
From: rusi <rustompm...@gmail.com>
Date: Mon, 8 Oct 2012 19:34:26 -0700 (PDT)
Local: Mon, Oct 8 2012 10:34 pm
Subject: Re: Insert item before each element of a list
On Oct 9, 7:06 am, Roy Smith <r...@panix.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]

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
rusi  
View profile  
 More options Oct 8 2012, 10:39 pm
Newsgroups: comp.lang.python
From: rusi <rustompm...@gmail.com>
Date: Mon, 8 Oct 2012 19:39:28 -0700 (PDT)
Local: Mon, Oct 8 2012 10:39 pm
Subject: Re: Insert item before each element of a list
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]

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
alex23  
View profile  
 More options Oct 8 2012, 11:11 pm
Newsgroups: comp.lang.python
From: alex23 <wuwe...@gmail.com>
Date: Mon, 8 Oct 2012 20:11:32 -0700 (PDT)
Local: Mon, Oct 8 2012 11:11 pm
Subject: Re: Insert item before each element of a list
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.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Duncan Booth  
View profile  
 More options Oct 9 2012, 7:40 am
Newsgroups: comp.lang.python
From: Duncan Booth <duncan.bo...@invalid.invalid>
Date: 9 Oct 2012 11:40:14 GMT
Local: Tues, Oct 9 2012 7:40 am
Subject: Re: Insert item before each element of a list

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]

> I appreciate any and all feedback.

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

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

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

--
Duncan Booth


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Oct 9 2012, 8:01 am
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Date: 09 Oct 2012 12:01:15 GMT
Local: Tues, Oct 9 2012 8:01 am
Subject: Re: Insert item before each element of a list

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Oct 9 2012, 8:55 am
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Tue, 09 Oct 2012 14:55:20 +0200
Local: Tues, Oct 9 2012 8:55 am
Subject: Re: Insert item before each element of a list

My post on gmane

http://thread.gmane.org/gmane.comp.python.general/718940/focus=718947

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.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mooremath...@gmail.com  
View profile  
 More options Oct 9 2012, 10:03 am
Newsgroups: comp.lang.python
From: mooremath...@gmail.com
Date: Tue, 9 Oct 2012 07:03:54 -0700 (PDT)
Local: Tues, Oct 9 2012 10:03 am
Subject: Re: Insert item before each element of a list

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hans Mulder  
View profile  
 More options Oct 11 2012, 6:22 pm
Newsgroups: comp.lang.python
From: Hans Mulder <han...@xs4all.nl>
Date: Fri, 12 Oct 2012 00:21:57 +0200
Local: Thurs, Oct 11 2012 6:21 pm
Subject: Re: Insert item before each element of a list
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Oct 11 2012, 7:39 pm
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Thu, 11 Oct 2012 19:38:41 -0400
Local: Thurs, Oct 11 2012 7:38 pm
Subject: Re: Insert item before each element of a list
On 10/11/2012 6:21 PM, Hans Mulder wrote:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Oct 11 2012, 10:16 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Date: 12 Oct 2012 02:16:20 GMT
Local: Thurs, Oct 11 2012 10:16 pm
Subject: Re: Insert item before each element of a list

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »