Concatenate two sorted arrays in Python using heapq



Examples:

 Input: arr1 = [1, 3, 4, 5] arr2 = [2, 4, 6, 8] Output: arr3 = [1, 2, 3, 4, 4 , 5, 6, 8] Input: arr1 = [5, 8, 9] arr2 = [4, 7, 8] Output: arr3 = [4, 5, 7, 8, 8, 9] 

This problem has an existing solution, please refer to the link

# Function to combine two sorted arrays

from heapq import merge

 

def mergeArray (arr1, arr2):

return list (merge (arr1, arr2))

  
# Driver function

if __ name__ = = "__ main __" :

arr1 = [ 1 , 3 , 4 , 5

arr2 = [ 2 , 4 , 6 , 8 ]

  print mergeArray (arr1, arr2)

Output:

 [1, 2, 3, 4, 4, 5, 6, 8]  

Module properties heapq?

This module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm. 
To create a heap, use a list initialized with [], or you can convert a populated list to a heap using the heapify () function. The following functions are provided: