Re: List insertion cost

2009-07-22 Thread Diez B. Roggisch
Lucas P Melo wrote: > Hello, > I would like to know how much it costs to insert an element into a list > using this operation: > a[2:2] = [ 1 ] > i. e, what is the complexity of the operation above (given that len(a) = > n)? O(n) I'd say. You need to copy nearly the whole list, and sometimes re-a

Re: List insertion cost

2009-07-21 Thread Lucas P Melo
Robert Kern wrote: O(n). Python lists are contiguous arrays in memory, and everything after the insertion point needs to be moved. Raymond Hettinger has a good talk about the implementation of Python lists and other container objects. http://www.youtube.com/watch?v=hYUsssClE94 http://www.pyco

Re: List insertion cost

2009-07-21 Thread Robert Kern
On 2009-07-21 14:21, Lucas P Melo wrote: Hello, I would like to know how much it costs to insert an element into a list using this operation: a[2:2] = [ 1 ] i. e, what is the complexity of the operation above (given that len(a) = n)? O(n). Python lists are contiguous arrays in memory, and every

Re: List insertion cost

2009-07-21 Thread Daniel Stutzbach
On Tue, Jul 21, 2009 at 2:21 PM, Lucas P Melo wrote: > I would like to know how much it costs to insert an element into a list > using this operation: > a[2:2] = [ 1 ] > i. e, what is the complexity of the operation above (given that len(a) = > n)? > O(n) If you want O(log n), you can use the b

List insertion cost

2009-07-21 Thread Lucas P Melo
Hello, I would like to know how much it costs to insert an element into a list using this operation: a[2:2] = [ 1 ] i. e, what is the complexity of the operation above (given that len(a) = n)? Thanks. -- http://mail.python.org/mailman/listinfo/python-list