I didn't know about heapq.merge before, that seems like an efficient way to go. Usually when I do k-way merges it's over a data stream, so I almost always want an iterator.
[0]
> list(heapq.merge([3, 2, 1], [3], [2, 3], [4, 5], [0, 1, 2]))
[0, 1, 2, 2, 3, 2, 1, 3, 3, 4, 5]
> sorted(itertools.chain([3, 2, 1], [3], [2, 3], [4, 5], [0, 1, 2]))
[0, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5]
[1]
> sorted(heapq.merge([3, 2, 1], [3], [2, 3], [4, 5], [0, 1, 2]))
[0, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5]
[2]
> list(itertools.chain([3, 2, 1], [3], [2, 3], [4, 5], [0, 1, 2]))
[3, 2, 1, 3, 2, 3, 4, 5, 0, 1, 2]
> list(heapq.merge([3, 2, 1], [3], [2, 3], [4, 5], [0, 1, 2]))
[0, 1, 2, 2, 3, 2, 1, 3, 3, 4, 5]I appreciate your comment
In this case certainly you should sort the multiple smaller arrays (in parallel), not the merged one.
>>> from heapq import merge
>>> def kmerge(*lists):
... return merge(*map(sorted, lists))
...
>>> list(kmerge([1,2,3],[6,5,4],[2,2]))
[1, 2, 2, 2, 3, 4, 5, 6]