How to concatenate 2 sorted arrays in Python using heapq?

| | | | | | | | | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

In this section, we will see how two sorted lists can be combined using the heapq module in Python. For example, if list1 = [10, 20, 30, 40] and list2 = [100, 200, 300, 400, 500], then after concatenation it will return list3 = [10, 20, 30, 40, 100, 200, 300 , 400, 500]

We will use the heapq module to accomplish this task. This module comes with Python as a standard library module. Therefore, we must import it before using it.

 import heapq 

The heapq module has some properties. It’s like below &



heapq.heapify method (repeatable)

It is used to transform an iterative dataset into a heap data structure.

The heapq.heappush method (heap, item)

This method is used to insert an item into the heap. Then copy the entire heap structure again.

The heapq.heappop method (heap)

This method is used to return and remove an item from the top of the heap and perform heapify on the rest.



The heapq.heappushpop method (heap, element)

This method is used to insert and paste an element into one statement.

The heapq.heapreplace method (heap, element)

This method is used to insert and paste an element into a single statement. It removes an element from the heap root, then inserts the element into the heap.

The heapq.nlargest (n, iterable, key = none) method

This method is used to return the n largest element from heap.

Method heapq.nsmallest (n, repeatable, key = None)

This method is used to return the n smallest element from the heap.

Code example < / h2>
 import heapq first_list = [45, 12, 63, 95, 74, 21, 20, 15, 36] second_list = [42, 13, 69, 54, 15] firs t_list = sorted (first_list) second_list = sorted (second_list) print (’First sorted list:’ + str (first_list)) print (’Second sorted list:’ + str (second_list)) final_list = list (heapq.merge (first_list, second_list)) print (’The final list:’ + str (final_list)) 

Output

 First sorted list: [12, 15, 20, 21, 36, 45, 63, 74, 95] Second sorted list: [13, 15, 42, 54, 69] The final list: [12, 13, 15, 15, 20, 21, 36, 42, 45, 54, 63, 69, 74, 95] 



👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from How to concatenate 2 sorted arrays in Python using heapq?, check other array Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Angelo Porretti

Warsaw | 2023-03-22

Maybe there are another answers? What How to concatenate 2 sorted arrays in Python using heapq? exactly means?. I am just not quite sure it is the best method

Walter Jackson

Warsaw | 2023-03-22

I was preparing for my coding interview, thanks for clarifying this - How to concatenate 2 sorted arrays in Python using heapq? in Python is not the simplest one. Will use it in my bachelor thesis

Schneider Richtgofen

Vigrinia | 2023-03-22

Thanks for explaining! I was stuck with How to concatenate 2 sorted arrays in Python using heapq? for some hours, finally got it done 🤗. Checked yesterday, it works!

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically