How Do i merge 2 Lists in an array to one Sorted Array in Python

Question:

I am trying to merge 2 Lists in python into one sorted List.
Please how is this possible?

My code is looking thus :

def MergeSortedArray(arr1, arr2):
    for i in range(len(arr1)):
        for j in range(len(arr2)):
            a = zip(i,j)
            
    return a


arr1 = [3,5,6,8]
arr2 = [1,2,9,7]

print(MergeSortedArray(arr1, arr2))

How do i get this to work? Please Help. I am getting this as error :

File "<string>", line 6
    return a
           ^
TabError: inconsistent use of tabs and spaces in indentation
>
Asked By: Mike

||

Answers:

import typing

def merge_sorted_array(a1: typing.List[int], a2: typing.List[int]) -> typing.List[int]:
    return sorted(a1 + a2)

The error you are getting has nothing to do with your problem. It means you are mixing tabs and spaces in your code, and the interpreter can not figure out the indentation because of it.

Answered By: Joris Schellekens
arr1 = sorted(arr1)
arr2 = sorted(arr2)

# Merging 2 sorted arrays
finalSortedarray = arr1 + arr2

# Sorting 2 arrays and Merging them
finalSortedarray = sorted(arr1 + arr2)

Hope this helps!

Answered By: Varsha Ramamurthy
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.