list.sort(func) speed

mackstann mack at incise.org
Sun Aug 31 17:37:33 EDT 2003


On Sun, Aug 31, 2003 at 02:08:43PM -0700, Chad Netzer wrote:
> On Sun, 2003-08-31 at 13:33, mackstann wrote:
> 
> > No, but the thought of inserting things in order did not quite occur to
> > me.  Can you give me an simple example of exactly how to go about this?
> 
> Don't have time for an example at the moment.  But the bisect module
> will do it for you.
> 
> help('bisect')
> help('bisect.insort')
> 
> The basic way in which it works (I assume, having not looked at the
> code), is a binary search algorithm, which is very fast.

Cool.

>>> from bisect import insort
>>> l = [(1,'old'), (2,'old'), (3,'old')]
>>> insort(l, (2,'new'))
>>> l
[(1, 'old'), (2, 'new'), (2, 'old'), (3, 'old')]

Thanks,
-- 
m a c k s t a n n  mack @ incise.org  http://incise.org
A long-forgotten loved one will appear soon.  Buy the negatives at any
price.





More information about the Python-list mailing list