Refer to the following code copied from solution 4 of this page - https://discuss.leetcode.com/topic/50450/slow-1-liner-to-fast-solutions/2:
streams = map(lambda u: ([u+v, u, v] for v in nums2), nums1)
stream = heapq.merge(*streams)
nums2, nums1 are lists of numbers.
Why does heapq.merge by default sort on u+v of the [u+v, u, v] lists? The u+v's across different lists in each generator are indeed in sorted order (because nums2 and nums1 are in ascending order), but I don't get how the heap.merge() knows to merge on u+v, the first element of the lists in the len(nums1) generators.