Program to Merge Two Lists and Sort it

Python Program to Merge Two Lists and Sort it | How to Create a Sorted Merged List of Two Unsorted Lists in Python?

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 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.

Lists in Python

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.

Given two lists the task is to merge the two given lists and sort them in Python

Combining Two Unsorted Lists and then Sort them Examples

String List:

Example 1:

Input:

given list one = ['hello', 'this', 'is', 'BTechGeeks']
given list two = ['online', 'platform', 'for', 'coding', 'students']

Output:

printing the merged list after sorting them = 
['BTechGeeks', 'coding', 'for', 'hello', 'is', 'online', 'platform', 'students', 'this']

Integer List:

Example 2

Input:

given list one = [17, 11, 13, 9, 8, 22, 29]
given list two = [3, 23, 29, 11, 98, 23, 15, 34, 37, 43, 47]

Output:

printing the merged list after sorting them = 
[3, 8, 9, 11, 11, 13, 15, 17, 22, 23, 23, 29, 29, 34, 37, 43, 47, 98]

How to Merge Two Lists in Python and Sort it?

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.

  • Using + operator and sort() function (Static Input)
  • Using + operator and sort() function (User Input)

Method #1: Using + operator and sort() function(Static Input)

Approach:

  • Give two lists input as static and store them in two separate variables.
  • Using the ‘+’ operator, merge both the first list and the second list, store it in a variable.
  • Sort the lists using the sort() function.
  • Print the list after sorting them.
  • The exit of Program.

i)String List

Write a Program to Merge Two Lists and Sort them on giving Static Input in Python?

# given first list
given_list1 = ['hello', 'this', 'is', 'BTechGeeks']
# given second list
given_list2 = ['online', 'platform', 'for', 'coding', 'students']
# merging both lists and storing it in a variable
mergedList = given_list1+given_list2
# sorting the merged list using sort() function
mergedList.sort()

# printing the merged list after sorting them
print('printing the merged list after sorting them = ')
print(mergedList)

Python Program to Merge Two Lists and Sort them(Static Input)

Output:

printing the merged list after sorting them = 
['BTechGeeks', 'coding', 'for', 'hello', 'is', 'online', 'platform', 'students', 'this']

ii)Integer List

Below is the implementation:

# given first list
given_list1 = [17, 11, 13, 9, 8, 22, 29]
# given second list
given_list2 = [3, 23, 29, 11, 98, 23, 15, 34, 37, 43, 47]
# merging both lists and storing it in a variable
mergedList = given_list1+given_list2
# sorting the merged list using sort() function
mergedList.sort()

# printing the merged list after sorting them
print('printing the merged list after sorting them = ')
print(mergedList)

Output:

printing the merged list after sorting them = 
[3, 8, 9, 11, 11, 13, 15, 17, 22, 23, 23, 29, 29, 34, 37, 43, 47, 98]

Method #2: Using + operator and sort() function (User Input)

Approach:

  • Scan the two lists using user input functions.
  • Using ‘+’ operator, merge both the first list, the second list and store them in a variable.
  • Sort the lists using the sort() function.
  • Print the sorted list.
  • The exit of Program.

i)String List

Scan the list using split() ,input() and list() functions.

Write a python program to merge two lists and sort it take input from the user and display the sorted merged list?

Below is the implementation to merge two lists and sort them

# scanning the first string list using split() and input() functions
given_list1 = list(input(
    'Enter some random string elements in list one separated by spaces = ').split())
# scanning the second string list using split() and input() functions
given_list2 = list(input(
    'Enter some random string elements in list one separated by spaces = ').split())
# merging both lists and storing it in a variable
mergedList = given_list1+given_list2
# sortin the merged list using sort() function
mergedList.sort()

# printing the merged list after sorting them
print('printing the merged list after sorting them = ')
print(mergedList)

Python Program to Merge Two Lists and Sort them(User Input)

Output:

Enter some random string elements in list one separated by spaces = hello this is btechgeeks
Enter some random string elements in list one separated by spaces = online coding platoform for coding students
printing the merged list after sorting them = 
['btechgeeks', 'coding', 'coding', 'for', 'hello', 'is', 'online', 'platoform', 'students', 'this']

ii)Integer List

Scan the list using split() ,map(),input() and list() functions.

map() function converts the every string element in the given list to integer.

Below is the implementation:

# scanning the first string list using split() and input() functions
given_list1 = list(map(int,input(
    'Enter some random string elements in list one separated by spaces = ').split()))
# scanning the second string list using split() and input() functions
given_list2 = list(map(int,input(
    'Enter some random string elements in list one separated by spaces = ').split()))
# merging both lists and storing it in a variable
mergedList = given_list1+given_list2
# sorting the merged list using sort() function
mergedList.sort()

# printing the merged list after sorting them
print('printing the merged list after sorting them = ')
print(mergedList)

Output:

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
Enter some random string elements in list one separated by spaces = 1 3 5 11 58 23 97 17 19 23 29 75 36 43
printing the merged list after sorting them = 
[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]

Similar Tutorials: