[Python-ideas] priorityqueue, sortedlist in collections?

Jason Orendorff jason.orendorff at gmail.com
Thu Mar 1 23:46:55 CET 2007


I wonder if anyone else finds the heapq and bisect APIs a little dated.

It seems like these things could be offered in a more intuitive and
featureful way by making them classes.  They could go in the
collections module:

  class priorityqueue:
      def __init__(self, elements=(), *,
          cmp=None, key=None, reversed=False)
      def add(self, element)
      def pop(self)  --> remove and return the min element
      def __iter__(self) --> (while self: yield self.pop())
      ...
      ... any other list methods that make sense
      ... for example, __len__ but not __getitem__
      ...

  class sortedlist:
      def __init__(self, elements=(), *,
          cmp=None, key=None, reversed=False)
      def add(self, element)  # insort

      # Methods involving searching are O(log N).
      def __contains__(self, element)
      def index(self, element)
      def remove(self, element)
      ...
      ... plus all the other read-only list methods,
      ... and the modifying list methods that make sense
      ...

The point is, most of the API comes from list and sort.  I think
they'd be easier to use/remember than what we have.

Once upon a time Tim mentioned (maybe semi-seriously) possibly adding
a Fibonacci heap container.  I think a plain old binary heap would be
good enough for starters.

I could do this.  Any interest?

-j



More information about the Python-ideas mailing list