Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31140

Re: Insert item before each element of a list

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'source.': 0.05; 'repeated': 0.07; 'python': 0.09; '[])': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'x),': 0.09; 'def': 0.10; 'anyway': 0.11; 'algorithm.': 0.16; 'appends': 0.16; 'module:': 0.16; 'n=3,': 0.16; 'oct': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:item': 0.16; 'traverse': 0.16; 'unneeded': 0.16; 'version?': 0.16; 'worst': 0.16; 'wrote:': 0.17; 'expanded': 0.17; 'typical': 0.17; 'yield': 0.17; 'jan': 0.18; '>>>': 0.18; 'equivalent': 0.20; 'import': 0.21; 'needed.': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(which': 0.26; 'creating': 0.26; 'expand': 0.26; 'extend': 0.26; 'am,': 0.27; 'header:X-Complaints-To:1': 0.28; 'subject:list': 0.28; 'run': 0.28; 'appending': 0.29; 'extending': 0.29; 'tail': 0.29; 'source': 0.29; 'call.': 0.30; 'lists': 0.31; 'to:addr :python-list': 0.33; 'version': 0.34; 'or,': 0.34; 'list': 0.35; 'faster': 0.35; 'pm,': 0.35; 'list.': 0.35; 'received:org': 0.36; 'except': 0.36; 'but': 0.36; 'does': 0.37; 'level': 0.37; 'two': 0.37; 'item': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'to:addr:python.org': 0.39; 'build': 0.39; 'header:Received:5': 0.40; 'matter': 0.61; 'different': 0.63; 'here': 0.65; '10000': 0.65; 'beat': 0.65; 'idiomatic': 0.84; 'prefers': 0.84; 'received:fios.verizon.net': 0.84; 'situations,': 0.84; 'subject:before': 0.84; 'modern,': 0.91; 'rusi': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Insert item before each element of a list
Date Thu, 11 Oct 2012 19:38:41 -0400
References <fc55bb2e-fdc0-405c-89b5-290aa4fc6c63@googlegroups.com> <mailman.1976.1349747963.27098.python-list@python.org> <roy-323E36.22065008102012@news.panix.com> <d0a5ccce-4a18-41e2-b804-795891db895b@lb2g2000pbc.googlegroups.com> <4ebfe84f-dfaa-40cb-88d9-414660d0c42f@ro10g2000pbc.googlegroups.com> <50774685$0$6943$e4fe514c@news2.news.xs4all.nl>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0
In-Reply-To <50774685$0$6943$e4fe514c@news2.news.xs4all.nl>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2074.1349998762.27098.python-list@python.org> (permalink)
Lines 63
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1349998762 news.xs4all.nl 6887 [2001:888:2000:d::a6]:51176
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31140

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web