import heapq tas = [] for i in range(5): heapq.heappush(tas, i) while not len(tas) == 0: print(heapq.heappop(tas), end=" ") # 0 1 2 3 4