- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to create a sorted merged list of two unsorted lists
In this tutorial, we are going to write a program that merges two lists and prints the resultant list in sorted order. Let's see some examples.
Input: list_1 = [1, 3, 2, 0, 3] list_2 = [20, 10, 23, 43, 56, -1] Output: [-1, 0, 1, 2, 3, 3, 10, 20, 23, 43, 56]
Input: list_1 = ["hafeez", "aslan"] list_2 = ["abc", "kareem", "b"] Output: ["abc", "aslan", "b", "hafeez", "kareem"]
Let's try to write the code with the following steps.
Algorithm
1. Initialize the lists. 2. Concatenate the two lists using + operator and store the result in a new variable. 3. Sort the resultant list with sort() method of the list. 4. Print the sorted list.
See the code.
Example
## initializing the lists list_1 = [1, 3, 2, 0, 3] list_2 = [20, 10, 23, 43, 56, -1] ## concatenating two lists new_list = list_1 + list_2 ## soring the new_list with sort() method new_list.sort() ## printing the sorted list print(new_list)
Output
If you run the above program, you will get the following output.
[-1, 0, 1, 2, 3, 3, 10, 20, 23, 43, 56]
We are executing the same program with different lists.
Example
## initializing the lists list_1 = ["hafeez", "aslan"] list_2 = ["abc", "kareem", "b"] ## concatenating two lists new_list = list_1 + list_2 ## soring the new_list with sort() method new_list.sort() ## printing the sorted list print(new_list)
Output
If you run the above program, you will get the following output.
['abc', 'aslan', 'b', 'hafeez', 'kareem']
Conclusion
If you have any doubts regarding the tutorial, mention them in the comment section.
- Related Articles
- Python program to create a sorted merged list of two unsorted list
- Java program to create a sorted merged array of two unsorted arrays
- Program to merge two sorted list to form larger sorted list in Python
- Merge Two Sorted Lists in Python
- Combining two sorted lists in Python
- Program to find median of two sorted lists in C++
- Python program to list the difference between two lists.
- Program to merge K-sorted lists in Python
- Program to interleave list elements from two linked lists in Python
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes in Python
- Python program to find Intersection of two lists?
- Merging two unsorted arrays in sorted order in C++.
- Python program to convert a list into a list of lists using a step value
- C# program to list the difference between two lists
- Python Program to Put Even and Odd elements in a List into Two Different Lists

Advertisements