Creating a Sorted Merged List of Two Unsorted Lists in Python Last Updated : 17 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Creating a sorted merged list of two unsorted lists involves combining the elements of both lists and sorting the resulting list in ascending order. For example: If we have list1 = [25, 18, 9] and list2 = [45, 3, 32] the output will be [3, 9, 18, 25, 32, 45].Using + OperatorThis method merges the two lists using the + operator which concatenates them and then combined list is sorted using the sort() method. Python a = [25, 18, 9, 41, 26, 31] b = [25, 45, 3, 32, 15, 20] # Merge and sort using + operator and sort() res = a + b res.sort() print(res) Output[3, 9, 15, 18, 20, 25, 25, 26, 31, 32, 41, 45] Explanation:+ operator concatenates l1 and l2 into a single list.sort() function rearranges the elements of the combined list in ascending order.Using heapq.merge()This method uses the merge() function from Python's heapq module which merges two sorted iterables into a single sorted iterable. The lists are first individually sorted then merged using heapq.merge(). Python import heapq a = [25, 18, 9, 41, 26, 31] b = [25, 45, 3, 32, 15, 20] # Merge and sort using heapq.merge() a.sort() b.sort() res = list(heapq.merge(a,b)) print(res) Output[3, 9, 15, 18, 20, 25, 25, 26, 31, 32, 41, 45] Explanation:Input lists (a and b) are sorted individually using sort().heapq.merge() function combines the two sorted lists into a single sorted iterable which is converted to a list using list(). Comment More infoAdvertise with us Next Article Python | Merge List with common elements in a List of Lists C chinmoy lenka Follow Improve Article Tags : Python python-list Python list-programs Practice Tags : pythonpython-list Similar Reads Python | Merge List with common elements in a List of Lists Given a list of list, we have to merge all sub-list having common elements. These type of problems are very frequent in College examinations and while solving coding competitions. Below are some ways to achieve this. Input: [[11, 27, 13], [11, 27, 55], [22, 0, 43], [22, 0, 96], [13, 27, 11], [13, 27 3 min read Merge two sorted arrays in Python using heapq Given two sorted arrays, the task is to merge them in a sorted manner. Examples: Input : arr1 = [1, 3, 4, 5] arr2 = [2, 4, 6, 8] Output : arr3 = [1, 2, 3, 4, 4, 5, 6, 8] Input : arr1 = [5, 8, 9] arr2 = [4, 7, 8] Output : arr3 = [4, 5, 7, 8, 8, 9] This problem has existing solution please refer Merge 2 min read Sort a list in Python without sort Function Sorting a list in Python means arranging the elements in a specific order (ascending or descending) using custom logic. For example, in a list like [5, 2, 9, 1], we can manually rearrange the elements as [1, 2, 5, 9]. Letâs explore different methods of sorting a list without using built-in sort func 3 min read Sort a list in Python without sort Function Sorting a list in Python means arranging the elements in a specific order (ascending or descending) using custom logic. For example, in a list like [5, 2, 9, 1], we can manually rearrange the elements as [1, 2, 5, 9]. Letâs explore different methods of sorting a list without using built-in sort func 3 min read Python - Sort list of list by specified index When working with a list of lists, we often need to sort the inner lists based on the values at a specific index. Sorting by a specified index is useful in the custom ordering of multidimensional data. In this article, we will explore different ways to sort a list of lists by a specified index in Py 3 min read Python - Sort list of list by specified index When working with a list of lists, we often need to sort the inner lists based on the values at a specific index. Sorting by a specified index is useful in the custom ordering of multidimensional data. In this article, we will explore different ways to sort a list of lists by a specified index in Py 3 min read Like