
- 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 program to sort and reverse a given list
Suppose we have a list of numbers in Python. We have to reverse and sort the lists using list operations but do not change the actual list. To reverse the list we have reverse() function for lists but if we use it, the list will be reversed in place. Similar for the sort() also. To maintain actual order we will use the reversed() function and sorted() function.
So, if the input is like l = [2,5,8,6,3,4,7,9], then the output will be [9, 7, 4, 3, 6, 8, 5, 2] [2, 3, 4, 5, 6, 7, 8, 9]
To solve this, we will follow these steps −
- rev := list from the output iterator from reversed function output
- display rev
- srt := sort the list l using sorted() function
- display srt
Example
Let us see the following implementation to get better understanding
def solve(l): rev = list(reversed(l)) print (rev) srt = sorted(l) print(srt) l = [2,5,8,6,3,4,7,9] solve(l)
Input
[2,5,8,6,3,4,7,9]
Output
[9, 7, 4, 3, 6, 8, 5, 2] [2, 3, 4, 5, 6, 7, 8, 9]
- Related Articles
- Golang Program to reverse a given linked list.
- Program to sort a given linked list into ascending order in python
- Program to reverse a linked list in Python
- Python program to Reverse a range in list
- Program to reverse a list by list slicing in Python
- Program to sort all elements in a given list and merge them into a string in Python
- Python program to sort a list of tuples alphabetically
- Java Program to Reverse a List
- Sort a List in reverse order in Java
- Program to reverse inner nodes of a linked list in python
- C program to sort a given list of numbers in ascending order using Bubble sort
- Program to reverse an array up to a given position in Python
- Python Program to Reverse only First N Elements of a Linked List
- Python program to sort a list of tuples by second Item
- Python Program to Sort A List Of Names By Last Name

Advertisements