This structure i have built for easier understanding and usability
class PriorityQueue:
"""
Implements a priority queue data structure.
"""
def __init__(self):
self.heap = []
self.count = 0
def push(self, item, priority):
entry = (priority, self.count, item)
heapq.heappush(self.heap, entry)
self.count += 1
def pop(self):
(_, _, item) = heapq.heappop(self.heap)
return item
def isEmpty(self):
return len(self.heap) == 0
I want to add a method which will return the list of items currently in this class such that it returns the list elements without the cost without actually popping each and every item
Is there any such way or i have to extract each element
Your method could look like this:
def items(self):
return list(item for _, _, item in self.heap)
This will iterate of the heap and build a new list containing references to the items in the heap without modifying the heap.
Note that if you items are mutable objects, modifying items in the list will also modify the references item in the heap. The may or may not be desirable.
Also note that this list will not be ordered by priority.