{"id":9254,"date":"2021-10-01T10:00:01","date_gmt":"2021-10-01T04:30:01","guid":{"rendered":"https:\/\/python-programs.com\/?p=9254"},"modified":"2021-11-22T18:33:31","modified_gmt":"2021-11-22T13:03:31","slug":"python-program-to-merge-two-lists-and-sort-it","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-merge-two-lists-and-sort-it\/","title":{"rendered":"Python Program to Merge Two Lists and Sort it | How to Create a Sorted Merged List of Two Unsorted Lists in Python?"},"content":{"rendered":"
Are you a job seeker and trying to find simple java programs for Interview?<\/a> This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.<\/p>\n There are many instances in our programming where we need to merge two unsorted lists and we might feel it difficult to write a program. If so, you need not worry anymore as we have discussed all on how to create a sorted merged list from two unsorted lists in python, methods for merging two lists and sorting them. In this Tutorial covers Each and every method for combining two unsorted lists and then how to obtain the sorted list considering enough examples.<\/p>\n A list is exactly what it sounds like: a container for various Python objects such as integers, words, values, and so on. In other programming languages, it is equal to an array. It is denoted with square brackets (and this is one of the attributes that differentiates it from tuples, which are separated by parentheses). It is also mutable, which means it can be changed or altered, as opposed to tuples, which are immutable.<\/p>\n Given two lists the task is to merge the two given lists and sort them in Python<\/p>\n String List:<\/strong><\/p>\n Example 1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Integer List:<\/strong><\/p>\n Example 2<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to merge the two given lists and sort them some of them are. This tutorial can be helpful while you are dealing with multiple lists. + Operator is used to merge and then sort them by defining a method called the sort method. You can check the examples for python programs in both cases be it static input or user input and how it gets the sorted list.<\/p>\n Approach:<\/strong><\/p>\n i)String List<\/strong><\/p>\n <\/p>\n Output:<\/strong><\/p>\n ii)Integer List<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n i)String List<\/strong><\/p>\n Scan the list using split() ,input() and list() functions.<\/p>\n Below is the implementation to merge two lists and sort them<\/p>\n <\/p>\n Output:<\/strong><\/p>\n ii)Integer List<\/strong><\/p>\n Scan the list using split() ,map(),input() and list() functions.<\/p>\n map() function converts the every string element in the given list to integer.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Similar Tutorials<\/strong>:<\/p>\n Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview. There are many instances in our programming where we need to merge two unsorted lists and we …<\/p>\n\n
Lists in Python<\/h2>\n
Combining Two Unsorted Lists and then Sort them Examples<\/h3>\n
given list one = ['hello', 'this', 'is', 'BTechGeeks']\r\ngiven list two = ['online', 'platform', 'for', 'coding', 'students']\r\n<\/pre>\n
printing the merged list after sorting them = \r\n['BTechGeeks', 'coding', 'for', 'hello', 'is', 'online', 'platform', 'students', 'this']\r\n<\/pre>\n
given list one = [17, 11, 13, 9, 8, 22, 29]\r\ngiven list two = [3, 23, 29, 11, 98, 23, 15, 34, 37, 43, 47]\r\n<\/pre>\n
printing the merged list after sorting them = \r\n[3, 8, 9, 11, 11, 13, 15, 17, 22, 23, 23, 29, 29, 34, 37, 43, 47, 98]\r\n<\/pre>\n
How to Merge Two Lists in Python and Sort it?<\/h2>\n
\n
Method #1: Using + operator and sort() function(Static Input)<\/h3>\n
\n
Write a Program to Merge Two Lists and Sort them on giving Static Input in Python?<\/h3>\n
# given first list\r\ngiven_list1 = ['hello', 'this', 'is', 'BTechGeeks']\r\n# given second list\r\ngiven_list2 = ['online', 'platform', 'for', 'coding', 'students']\r\n# merging both lists and storing it in a variable\r\nmergedList = given_list1+given_list2\r\n# sorting the merged list using sort() function\r\nmergedList.sort()\r\n\r\n# printing the merged list after sorting them\r\nprint('printing the merged list after sorting them = ')\r\nprint(mergedList)\r\n<\/pre>\n
printing the merged list after sorting them = \r\n['BTechGeeks', 'coding', 'for', 'hello', 'is', 'online', 'platform', 'students', 'this']\r\n<\/pre>\n
# given first list\r\ngiven_list1 = [17, 11, 13, 9, 8, 22, 29]\r\n# given second list\r\ngiven_list2 = [3, 23, 29, 11, 98, 23, 15, 34, 37, 43, 47]\r\n# merging both lists and storing it in a variable\r\nmergedList = given_list1+given_list2\r\n# sorting the merged list using sort() function\r\nmergedList.sort()\r\n\r\n# printing the merged list after sorting them\r\nprint('printing the merged list after sorting them = ')\r\nprint(mergedList)\r\n<\/pre>\n
printing the merged list after sorting them = \r\n[3, 8, 9, 11, 11, 13, 15, 17, 22, 23, 23, 29, 29, 34, 37, 43, 47, 98]\r\n<\/pre>\n
Method #2: Using + operator and sort() function (User Input)<\/h3>\n
\n
Write a python program to merge two lists and sort it take input from the user and display the sorted merged list?<\/h3>\n
# scanning the first string list using split() and input() functions\r\ngiven_list1 = list(input(\r\n 'Enter some random string elements in list one separated by spaces = ').split())\r\n# scanning the second string list using split() and input() functions\r\ngiven_list2 = list(input(\r\n 'Enter some random string elements in list one separated by spaces = ').split())\r\n# merging both lists and storing it in a variable\r\nmergedList = given_list1+given_list2\r\n# sortin the merged list using sort() function\r\nmergedList.sort()\r\n\r\n# printing the merged list after sorting them\r\nprint('printing the merged list after sorting them = ')\r\nprint(mergedList)\r\n<\/pre>\n
Enter some random string elements in list one separated by spaces = hello this is btechgeeks\r\nEnter some random string elements in list one separated by spaces = online coding platoform for coding students\r\nprinting the merged list after sorting them = \r\n['btechgeeks', 'coding', 'coding', 'for', 'hello', 'is', 'online', 'platoform', 'students', 'this']\r\n<\/pre>\n
# scanning the first string list using split() and input() functions\r\ngiven_list1 = list(map(int,input(\r\n 'Enter some random string elements in list one separated by spaces = ').split()))\r\n# scanning the second string list using split() and input() functions\r\ngiven_list2 = list(map(int,input(\r\n 'Enter some random string elements in list one separated by spaces = ').split()))\r\n# merging both lists and storing it in a variable\r\nmergedList = given_list1+given_list2\r\n# sorting the merged list using sort() function\r\nmergedList.sort()\r\n\r\n# printing the merged list after sorting them\r\nprint('printing the merged list after sorting them = ')\r\nprint(mergedList)\r\n<\/pre>\n
Enter some random string elements in list one separated by spaces = 48 79 23 15 48 19 7 3 5 4 2 98 75 123 456 81 8 5\r\nEnter some random string elements in list one separated by spaces = 1 3 5 11 58 23 97 17 19 23 29 75 36 43\r\nprinting the merged list after sorting them = \r\n[1, 2, 3, 3, 4, 5, 5, 5, 7, 8, 11, 15, 17, 19, 19, 23, 23, 23, 29, 36, 43, 48, 48, 58, 75, 75, 79, 81, 97, 98, 123, 456]\r\n<\/pre>\n
\n