implementation of priority queue using linear array insertion on extract minmax node o1 create a priority queue with items list or iterable if items is not passed create empty priority queue self priorityqueuelist if items is none return if priorities is none priorities itertools repeatnone for item priority in zipitems priorities self pushitem prioritypriority def reprself return priorityqueue r formatself priorityqueuelist def sizeself return lenself priorityqueuelist def pushself item prioritynone priority item if priority is none else priority node priorityqueuenodeitem priority for index current in enumerateself priorityqueuelist if current priority node priority self priorityqueuelist insertindex node return when traversed complete queue self priorityqueuelist appendnode def popself remove and return the first node from the queue return self priorityqueuelist pop data create a priority queue with items list or iterable if items is not passed create empty priority queue return size of the priority queue push the item in the priority queue if priority is not given priority is set to the value of item when traversed complete queue remove and return the item with the lowest priority remove and return the first node from the queue
import itertools


class PriorityQueueNode:
    def __init__(self, data, priority):
        self.data = data
        self.priority = priority

    def __repr__(self):
        return "{}: {}".format(self.data, self.priority)


class PriorityQueue:
    def __init__(self, items=None, priorities=None):
        
        self.priority_queue_list = []
        if items is None:
            return
        if priorities is None:
            priorities = itertools.repeat(None)
        for item, priority in zip(items, priorities):
            self.push(item, priority=priority)

    def __repr__(self):
        return "PriorityQueue({!r})".format(self.priority_queue_list)

    def size(self):
        
        return len(self.priority_queue_list)

    def push(self, item, priority=None):
        
        priority = item if priority is None else priority
        node = PriorityQueueNode(item, priority)
        for index, current in enumerate(self.priority_queue_list):
            if current.priority < node.priority:
                self.priority_queue_list.insert(index, node)
                return
        
        self.priority_queue_list.append(node)

    def pop(self):
        
        
        return self.priority_queue_list.pop().data