peek

Can I get an item from a PriorityQueue without removing it yet?

Can I get an item from a PriorityQueue without removing it yet? Question: I want to get the next item in queue but I don’t want to dequeue it. Is it possible in Python’s queue.PriorityQueue? From the docs, I don’t see how can it be done Asked By: Jiew Meng || Source Answers: Indexing the …

Total answers: 7

How to look ahead one element (peek) in a Python generator?

How to look ahead one element (peek) in a Python generator? Question: I can’t figure out how to look ahead one element in a Python generator. As soon as I look it’s gone. Here is what I mean: gen = iter([1,2,3]) next_value = gen.next() # okay, I looked forward and see that next_value = 1 …

Total answers: 18

Peeking in a heap in python

Peeking in a heap in python Question: What is the official way of peeking in a python heap as created by the heapq libs? Right now I have def heappeak(heap): smallest = heappop(heap) heappush(heap, smallest) return smallest which is arguably, not very nice. Can I always assume that heap[0] is the top of the heap …

Total answers: 2