Heap Functions

heapq.merge()

Lazily merge inputs that are already sorted in the chosen direction. It does not sort unsorted inputs; reverse=True requires descending inputs.

Formal Syntax & Structure

python syntaxpython
heapq.merge(*iterables, key=None, reverse=False)

Parameters & Attributes

NameTypeDescription
iterablesiterableZero or more input iterables.
keycallable or NoneOptional key function used for comparison or grouping.
reverseboolUse descending order when true.

Return Value

iterator

The iterator result described above.

Working Example

Example Snippetpython
import heapqprint(list(heapq.merge([1,4],[2,3])))
Python Reference