Creating a sorted merged list of two unsorted lists in Python

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.



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.