python bisect questions

Terry Reedy tjreedy at
Thu Apr 3 23:24:25 CEST 2008

<ankitks.mital at> wrote in message 
news:6bb6927b-f553-40db-a142-2ce86b9e819f at
|I am week on functional programming, and having hard time
| understanding this:
| class myPriorityQueue:
|      def __init__(self, f=lamda x:x):
|              self.A = []
|              self.f = f
|      def append(self, item)
|              bisect.insort(self.A, (self.f(item), item))
|    ............
| now I know we are inserting items(user defined type objects) in list A
| base on sorting order provided by function A.
| but what I don't understand is bisect command
| what does bisect.insort(self.A, (self.f(item), item)) doing

The snippet is missing 'import bisect'.  The module is documented in the 
Lib Ref.  Or, in the interpreter, help(bisect.insort) redirects you to 
help(bisect.insort_right), which will answer your question.

| isn't it is returning truple of (self.f(item), item)).

no, see doc

| why it is not
| biset.insort(self.A, item)
| A.sort(f)

Efficiency, given that self.A is already sorted.


More information about the Python-list mailing list