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


Groups > comp.lang.python > #65574

Re: Question about `list.insert`

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Question about `list.insert`
Date 2014-02-06 21:48 -0500
References <4041bba7-91bc-4803-9150-2fcf14ecb5a9@googlegroups.com> <52F42BDC.7000907@mrabarnett.plus.com>
Newsgroups comp.lang.python
Message-ID <mailman.6470.1391741345.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 2/6/2014 7:42 PM, MRAB wrote:
> On 2014-02-06 23:59, cool-RR 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?
>>
> If it needs more space it resizes. It then moves the items.

The OP apparently knows that there is usually extra space at the right 
(end), so that reallocation is usually not needed. He wanted to know 
whether extra space is also kept at the left (beginning) to make left 
appends as efficient as right appends. This has been proposed and the 
answer was to use collections.deque if it really matters. CPython lists 
are asymmetric re-sizable stacks

-- 
Terry Jan Reedy

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