Open In App

Creating a sorted merged list of two unsorted lists in Python

Improve
Improve
Like Article
Like
Save
Share
Report

We need to take two lists in Python and merge them into one. Finally, we display the sorted list.

Examples:

Input : 
list1 = [25, 18, 9, 41, 26, 31]
list2 = [25, 45, 3, 32, 15, 20]
Output :
[3, 9, 15, 18, 20, 25, 25, 26, 31, 32, 41, 45]

Input : 
list1 = ["suraj", "anand", "gaurav", "aman", "kishore"]
list2 = ["rohan", "ram", "mohan", "priya", "komal"]
Output :
['aman', 'anand', 'gaurav', 'kishore', 'komal', 
'mohan', 'priya', 'ram', 'rohan', 'suraj']

The plus operator “+” in Python helps us to merge two lists, be it a list of string or integers or mixture of both. Finally, we sort the list using the sort() function.




# Python program to merge and sort two list
def Merge(list1, list2):
    final_list = list1 + list2
    final_list.sort()
    return(final_list)
  
# Driver Code
list1 = [25, 18, 9, 41, 26, 31]
list2 = [25, 45, 3, 32, 15, 20]
print(Merge(list1, list2))


Output:

[3, 9, 15, 18, 20, 25, 25, 26, 31, 32, 41, 45]

Last Updated : 21 Nov, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads