I wrote a function that adds items in a list, but my professor says to try and write it in a simpler way because he says I take too many steps. Is there any other way to write this?
def additems(self, item):
"""Add <item> to this PriorityQueue.
@type self: PriorityQueue
@type item: object
@rtype: None
>>> pq = PriorityQueue()
>>> pq.additems("yellow")
>>> pq.additems("blue")
>>> pq.additems("red")
>>> pq.additems("green")
>>> pq._items
['blue', 'green', 'red', 'yellow']
"""
# TODO
#only insert if the list isn't already empty
if not self.is_empty():
i = 0
#Look for the lowest priority place to insert the item
while i < len(self._items):
if item <= self._items[i]:
self._items.insert(i, item)
#exit the loop if it inserts
i = len(self._items)
elif item >= self._items[-1]:
self._items.append(item)
#exit the loop if it inserts
i = len(self._items)
i += 1
else:
self._items.append(item)