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


Groups > comp.lang.python > #65601

Re: Question about `list.insert`

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'see:': 0.07; 'skip:` 10': 0.07; 'subject:Question': 0.07; 'append': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; "'from": 0.16; '2.59': 0.16; 'collections': 0.16; 'curious.': 0.16; 'none);': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:`': 0.16; 'wrote:': 0.18; 'memory': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; "i'm": 0.30; '(which': 0.31; '100000': 0.31; 'there,': 0.34; 'skip:d 20': 0.34; 'list': 0.37; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'entire': 0.61; 'first': 0.61; 'more': 0.64; '10000': 0.68; 'saving': 0.69; '100': 0.79
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Question about `list.insert`
Date Fri, 07 Feb 2014 09:25:10 +0100
Organization None
References <4041bba7-91bc-4803-9150-2fcf14ecb5a9@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849410.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.6487.1391761524.18130.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391761524 news.xs4all.nl 2970 [2001:888:2000:d::a6]:55097
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:65601

Show key headers only | View raw


cool-RR wrote:

> I'm curious. If I append an item to a list from the left using
> `list.insert`, will Python always move the entire list one item to the
> right (which can be super-slow) or will it check first to see whether it
> can just allocate more memory to the left of the list and put the item
> there, saving a lot of resources?

Let's see:

$ python3.4 -m timeit -s 'a = [None]' 'a.insert(0, None); del a[0]'
1000000 loops, best of 3: 0.596 usec per loop
$ python3.4 -m timeit -s 'a = [None]*1000' 'a.insert(0, None); del a[0]'
100000 loops, best of 3: 2.59 usec per loop
$ python3.4 -m timeit -s 'a = [None]*10000' 'a.insert(0, None); del a[0]'
10000 loops, best of 3: 24.7 usec per loop
$ python3.4 -m timeit -s 'a = [None]*100000' 'a.insert(0, None); del a[0]'
1000 loops, best of 3: 508 usec per loop
$ python3.4 -m timeit -s 'a = [None]*1000000' 'a.insert(0, None); del a[0]'
100 loops, best of 3: 7.61 msec per loop

$ python3.4 -m timeit -s 'from collections import deque; a = 
deque([None]*1000000)' 'a.appendleft(None); a.popleft()'
1000000 loops, best of 3: 0.263 usec per loop

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


Thread

Question about `list.insert` cool-RR <ram.rachum@gmail.com> - 2014-02-06 15:59 -0800
  Re: Question about `list.insert` Terry Reedy <tjreedy@udel.edu> - 2014-02-06 19:40 -0500
  Re: Question about `list.insert` MRAB <python@mrabarnett.plus.com> - 2014-02-07 00:42 +0000
  Re: Question about `list.insert` Terry Reedy <tjreedy@udel.edu> - 2014-02-06 21:48 -0500
  Re:Question about `list.insert` Dave Angel <davea@davea.name> - 2014-02-06 21:54 -0500
    Re: Question about `list.insert` Roy Smith <roy@panix.com> - 2014-02-06 22:00 -0500
      Re: Question about `list.insert` Rustom Mody <rustompmody@gmail.com> - 2014-02-06 19:08 -0800
      Re: Question about `list.insert` Tim Chase <python.list@tim.thechases.com> - 2014-02-06 21:11 -0600
        Re: Question about `list.insert` Roy Smith <roy@panix.com> - 2014-02-06 22:12 -0500
          Re: Question about `list.insert` Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-02-07 17:18 +1300
      Re: Question about `list.insert` Chris Angelico <rosuav@gmail.com> - 2014-02-07 14:14 +1100
      Re: Question about `list.insert` Chris Angelico <rosuav@gmail.com> - 2014-02-07 14:20 +1100
        Re: Question about `list.insert` Rustom Mody <rustompmody@gmail.com> - 2014-02-06 19:29 -0800
          Re: Question about `list.insert` Chris Angelico <rosuav@gmail.com> - 2014-02-07 14:45 +1100
      Re: Question about `list.insert` Asaf Las <roegltd@gmail.com> - 2014-02-06 19:28 -0800
  Re: Question about `list.insert` Dan Stromberg <drsalists@gmail.com> - 2014-02-06 20:52 -0800
    Re: Question about `list.insert` Asaf Las <roegltd@gmail.com> - 2014-02-06 21:18 -0800
  Re: Question about `list.insert` Peter Otten <__peter__@web.de> - 2014-02-07 09:25 +0100

csiph-web