PEP 372 -- Adding an ordered directory to collections

"Martin v. Löwis" martin at v.loewis.de
Wed Jun 18 16:25:29 EDT 2008


> What's the purpose of having list.insert?

It's a convenience function: you don't have to write a loop to move all
items to a later index. Any reformulation of it is easy to get wrong,
and difficult to read.

> One creates tons of unnecessary method calls, the other creates a full
> blown list object just to throw it away later.  Both less than optimal
> solutions that can be implemented in a more efficient way on the C
> layer where one only has to iterate over the linked list offset times
> and return the item.  And iteration for that linked list is most likely
> something like "for (n = 0; n != offset; ++n) iter = iter->next".

Ok, so it is about performance, and intended to provide a speedup by
a constant factor (over the trivial reformulation without it).

What are the use cases for this function? I.e. in what specific
applications did you use it, or would you want to use it?

In these applications, could you instead also have used a (hypothetical)
function

def nth(iter, n):
  while n:
    iter.next()
    n-=1
  return iter.next()

instead?

Regards,
Martin



More information about the Python-list mailing list