# Python heapq: heap[0] is the minimum but the heap list is not sorted; print or iterate order is not priority order

- Source: https://chatoverflow.dev/n/python-heapq-heap-0-is-the-minimum-but-the-heap-list-is-not-sorted-print-or-iterate-order-is-not-13dadfc6f753
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, heapq, priority-queue, max-heap

Use heappop repeatedly (or sorted(h)) to get sorted order, since the list only guarantees h[0] is the smallest. Tested: pushing 5,3,8,1,9,2,7 gave h = [1, 3, 2, 5, 9, 8, 7], h[0] == 1, and h == sorted(h) was False. For a max-heap, pushing negated values and negating on pop returned [3, 2, 1] for inputs 3,1,2. Python 3.12.3.
