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


Groups > comp.lang.python > #65592

Re: Question about `list.insert`

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <drsalists@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'position,': 0.05; 'mentioned,': 0.07; 'skip:` 10': 0.07; 'subject:Question': 0.07; 'append': 0.09; 'assuming': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'cc:name:python list': 0.16; 'curious.': 0.16; 'fancy': 0.16; 'inserting': 0.16; 'rather,': 0.16; 'subject:`': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'feb': 0.22; 'memory': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'sort': 0.25; 'header:In-Reply-To:1': 0.27; 'list:': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '(which': 0.31; 'implemented': 0.33; 'there,': 0.34; 'received:google.com': 0.35; 'add': 0.35; 'hi,': 0.36; 'url:org': 0.36; 'list': 0.37; 'list.': 0.37; 'pm,': 0.38; 'sure': 0.39; 'entire': 0.61; "you're": 0.61; 'first': 0.61; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'saving': 0.69
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=6ydIrrk7Vq9HngmwZ4BRrKYGdhCAs7irfCCrATlT4AM=; b=wzeEIuwdLgD7BxtJO7EzZp/ecvCkqQSfFaN4X+6Hj8+H8JiOs6xwZRJcic7dWeHkW+ 0sddB7y/Jm0BFO3soCeXOW6+DqiTYrj6AZkwf8muOgW+q1Km7yOS5DHMAboPcRvUqAvN eIEfrKN+Sqq/yZJ5f2hpLmmSdurKXagp9NV3Fe0ejXto96E3HDsb8tdl46O9JVltKUm5 nKnC+IM47wFeYMqZt82WWrsEyXePoKlvToU6yiNhmPOiK9En2jNDLpCFp2k+E7tacU1U gjLzROYVNEu7DmS+rk3CwxMN87BGzWeXK4O1mpREq0vE5Q6ZwU4DMQbiISNIOHSEGUWr 5x0w==
MIME-Version 1.0
X-Received by 10.194.186.204 with SMTP id fm12mr8838833wjc.27.1391748744674; Thu, 06 Feb 2014 20:52:24 -0800 (PST)
In-Reply-To <4041bba7-91bc-4803-9150-2fcf14ecb5a9@googlegroups.com>
References <4041bba7-91bc-4803-9150-2fcf14ecb5a9@googlegroups.com>
Date Thu, 6 Feb 2014 20:52:24 -0800
Subject Re: Question about `list.insert`
From Dan Stromberg <drsalists@gmail.com>
To cool-RR <ram.rachum@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc Python List <python-list@python.org>
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.6480.1391748751.18130.python-list@python.org> (permalink)
Lines 22
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391748751 news.xs4all.nl 2869 [2001:888:2000:d::a6]:46205
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:65592

Show key headers only | View raw


On Thu, Feb 6, 2014 at 3:59 PM, cool-RR <ram.rachum@gmail.com> wrote:
> Hi,
>
> 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?

I'm pretty sure it'll slide all the existing elements right one
position, and add at the leftmost position just opened up - assuming
you're inserting at position 0.

As was already mentioned, collections.deque is good for this sort of
thing.  It's implemented as a fancy doubly-linked list. Or rather, a
doubly-linked list of smallish arrays/lists.

For a singly-linked list:
http://stackoverflow.com/questions/280243/python-linked-list
http://stromberg.dnsalias.org/~strombrg/linked-list/

HTH

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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