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: 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 To: cool-RR Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Thu, Feb 6, 2014 at 3:59 PM, cool-RR wrote: > Hi, > > I'm curious. If I append an item to a list from the left using `list.inse= rt`, will Python always move the entire list one item to the right (which c= an be super-slow) or will it check first to see whether it can just allocat= e 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