- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python Program to Merge Two Lists and Sort it
When it is required to merge two lists and sort them, a method can be defined that sorts the list using ‘sort’ method.
Below is the demonstration of the same −
def merge_list(list_1, list_2): merged_list = list_1 + list_2 merged_list.sort() return(merged_list) list_1 = [20, 18, 9, 51, 48, 31] list_2 = [28, 33, 3, 22, 15, 20] print("The first list is :") print(list_1) print("The second list is :") print(list_2) print(merge_list(list_1, list_2))
The first list is : [20, 18, 9, 51, 48, 31] The second list is : [28, 33, 3, 22, 15, 20] [3, 9, 15, 18, 20, 20, 22, 28, 31, 33, 48, 51]
A method named ‘merge_list’ is defined that takes two lists as parameters.
The two lists are concatenated using the ‘+’ operator.
It is assigned to a variable.
The sort method is used to sort the final result.
Outside the method, two lists are defined, and displayed on the console.
The method is called by passing these two lists.
The output is displayed on the console.
- Related Articles
- Java Program to Merge two lists
- Merge Two Sorted Lists in Python
- Program to merge K-sorted lists in Python
- Python Program for Merge Sort
- Program to merge in between linked lists in Python
- Merge Sort for Linked Lists using C++.
- Python Program for Iterative Merge Sort
- Python program to merge two Dictionaries
- Python Program to Merge two Tuples
- Program to merge intervals and sort them in ascending order in Python
- C++ Program to Implement Merge Sort
- Merge two sorted linked lists using C++.
- Merge k Sorted Lists in Python
- Python program to find Intersection of two lists?
- C program to sort an array by using merge sort