# Merge two sorted arrays in Python using heapq

Given two sorted arrays, the task is to merge them in a sorted manner.

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 existing solution please refer Merge two sorted arrays link. We will solve this problem in python using **heapq.merge()** in a single line of code.

`# Function to merge 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) ` |

*chevron_right*

*filter_none*

Output:

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

### Properties of heapq module ?

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 to [], or you can transform a populated list into a heap via function heapify().The following functions are provided:

Push the value item onto the heap, maintaining the heap invariant.__heapq.heappush(heap,item) :__Pop and return the smallest item from the heap, maintaining the heap invariant. If the heap is empty,__heapq.heappop(heap) :__**IndexError**is raised. To access the smallest item without popping it, use heap[0].Push item on the heap, then pop and return the smallest item from the heap. The combined action runs more efficiently than heappush() followed by a separate call to heappop().__heapq.heappushpop(heap, item) :__Transform list x into a heap, in-place, in linear time.__heapq.heapify(x) :__Merge multiple sorted inputs into a single sorted output (for example, merge timestamped entries from multiple log files). Returns an iterator over the sorted values.__heapq.merge(*iterables) :__- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Merge k sorted arrays | Set 2 (Different Sized Arrays)
- Merge 3 Sorted Arrays
- Merge two sorted arrays
- Merge k sorted arrays | Set 1
- Merge two sorted arrays with O(1) extra space
- Merge two sorted arrays in constant space using Min Heap
- Merge K sorted arrays | Set 3 ( Using Divide and Conquer Approach )
- Merge K sorted arrays of different sizes | ( Divide and Conquer Approach )
- Heap queue (or heapq) in Python
- Python heapq to find K'th smallest element in a 2D array
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Python | Find common elements in three sorted arrays by dictionary intersection
- Sorted merge in one array
- Merge K sorted linked lists | Set 1

This article is contributed by **Shashank Mishra (Gullu)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.