List insertion cost

L

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.
 
D

Diez B. Roggisch

Lucas said:
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-allocate it's storage (they are internally stored as arrays of object
references)

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top