Merge two sorted lists python

Many a times we encounter a problem where we wish to use the merge function of merge sort and is a classical problem that occurs many times while doing competitive programming. This type of problem when known shorter and compact way to perform them are always quite handy.

Let’s discuss certain ways of combining two sorted list in Python.

Method #1 : Naive Method

Merge operation of merge sort can be performed using the naive method which has also been discussed earlier. We check for the smaller of two element on the current index and increment the index of the list whose no. is encountered. When either of the list gets exhausted, the other list is appended to the end of merged list.

test_list1 = [1, 5, 6, 9, 11]

test_list2 = [3, 4, 7, 8, 10]

print ["The original list 1 is : " + str[test_list1]]

print ["The original list 2 is : " + str[test_list2]]

size_1 = len[test_list1]

size_2 = len[test_list2]

res = []

i, j = 0, 0

while i < size_1 and j < size_2:

    if test_list1[i] < test_list2[j]:

      res.append[test_list1[i]]

      i += 1

    else:

      res.append[test_list2[j]]

      j += 1

res = res + test_list1[i:] + test_list2[j:]

print ["The combined sorted list is : " + str[res]]

Output:

The original list 1 is : [1, 5, 6, 9, 11]
The original list 2 is : [3, 4, 7, 8, 10]
The combined sorted list is : [1, 3, 4, 5, 6, 7, 8, 9, 10, 11]

 
Method #2 : Using sorted[]

This function can be used to perform this task in just a 1 line but will take more time internally. It may have more time complexity as we append one list to another and again sort the resultant list. Should be used if we need to save the coding time.

test_list1 = [1, 5, 6, 9, 11]

test_list2 = [3, 4, 7, 8, 10]

print ["The original list 1 is : " + str[test_list1]]

print ["The original list 2 is : " + str[test_list2]]

res = sorted[test_list1 + test_list2]

print ["The combined sorted list is : " + str[res]]

Output:

The original list 1 is : [1, 5, 6, 9, 11]
The original list 2 is : [3, 4, 7, 8, 10]
The combined sorted list is : [1, 3, 4, 5, 6, 7, 8, 9, 10, 11]

 
Method #3 : Using heapq.merge[]

Python also offers the inbuilt function to perform this particular task and performs the similar working in background as merge in naive method and should be used when wanting to deal with this kind of problem.

from heapq import merge

test_list1 = [1, 5, 6, 9, 11]

test_list2 = [3, 4, 7, 8, 10]

print ["The original list 1 is : " + str[test_list1]]

print ["The original list 2 is : " + str[test_list2]]

res = list[merge[test_list1, test_list2]]

print ["The combined sorted list is : " + str[res]]

Output:

The original list 1 is : [1, 5, 6, 9, 11]
The original list 2 is : [3, 4, 7, 8, 10]
The combined sorted list is : [1, 3, 4, 5, 6, 7, 8, 9, 10, 11]


How do you merge two lists and sorts in python?

Sort and Merge Two Lists using sort[] Method.
Define the two variables to assign the empty lists..
Now takes the number of inputs for the first and second list..
Now merge both lists using the '+' operator..
Use the built-in sort[] method to sort the newly created list..

How do I merge two arrays in sorted order in python?

The idea is to use Merge function of Merge sort..
Create an array arr3[] of size n1 + n2..
Simultaneously traverse arr1[] and arr2[]. ... .
If there are remaining elements in arr1[] or arr2[], copy them also in arr3[]..

How do I merge two linked lists?

[1] Create a new head pointer to an empty linked list. [2] Check the first value of both linked lists. [3] Whichever node from L1 or L2 is smaller, append it to the new list and move the pointer to the next node. [4] Continue this process until you reach the end of a linked list.

How do you merge lists in python?

One simple and popular way to merge[join] two lists in Python is using the in-built append[] method of python. The append[] method in python adds a single item to the existing list. It doesn't return a new list of items. Instead, it modifies the original list by adding the item to the end of the list.

Chủ Đề