all Python 3heapqfunction
heapq.merge()
merging
Merges several already sorted iterables into one sorted lazy iterator without loading everything into memory.
Syntax
heapq.merge(*iterables, key=None, reverse=False) -> iterator
The inputs must already be sorted, and nobody checks: on unsorted data merge silently returns an unordered result. It returns an iterator, not a list, so it can be consumed only once. With reverse=True every input must be sorted in descending order. If the data fits in memory, sorted(a + b) is simpler and just as good.
Examples
import heapq
print(list(heapq.merge([1, 4, 7], [2, 5, 8], [3, 6]))) # → [1, 2, 3, 4, 5, 6, 7, 8]
import heapq
print(list(heapq.merge([3, 1, 2], [0]))) # → [0, 3, 1, 2] — вход не отсортирован, ошибки нет
import heapq
words = heapq.merge(['ccc', 'a'], ['bb'], key=len, reverse=True)
print(list(words)) # → ['ccc', 'bb', 'a']
See also
Open in the full glossarydocs.python.orgReport an inaccuracy