Python program for sort list in ascending and descending order; In this python tutorial, we would love to share with you how to sort the elements of a list in Ascending and Descending order in Python.
Use the python inbuilt method name sort(), which is used to sort the elements/objects of the list in Ascending and Descending Order.
Basic Syntax of sort method:
list.sort()
Python Program for Sort List in Ascending and Descending Order
- Python Program to Sort List Elements in Ascending Order
- Python Program to Sort List Elements in Descending Order
Python Program to Sort List Elements in Ascending Order
# List of integers num = [100, 200, 500, 600, 300] # sorting and printing num.sort() #print print(num) # List of float numbers fnum = [100.43, 50.72, 90.65, 16.00, 04.41] # sorting and printing fnum.sort() #print print(fnum) # List of strings str = ["Test", "My", "Word", "Tag", "Has"] # sorting and printing str.sort() #print print(str)
After executing the python program, the output will be:
[100, 200, 300, 500, 600] [4.41, 16.0, 50.72, 90.65, 100.43] ['Has', 'My', 'Tag', 'Test', 'Word']
As you know above, how to sort list elements in ascending order. Now you will read how to sort a list in descending order by using sort() method.
You pass reverse=True as an argument with sort() method to sort a list elements in descending order.
You can see the following program to sort a list element in descending order.
Python Program to Sort List Elements in Descending Order
# List of integers num = [100, 200, 500, 600, 300] # sorting and printing num.sort(reverse=True) #print print(num) # List of float numbers fnum = [100.43, 50.72, 90.65, 16.00, 04.41] # sorting and printing fnum.sort(reverse=True) #print print(fnum) # List of strings str = ["Test", "My", "Word", "Tag", "Has"] # sorting and printing str.sort(reverse=True) #print print(str)
After executing the program, the output will be:
[600, 500, 300, 200, 100] [100.43, 90.65, 50.72, 16.0, 4.41] ['Word', 'Test', 'Tag', 'My', 'Has']
Recommended Python String Programs
- Python Program to Convert Uppercase to Lowercase
- Convert String Lowercase to Uppercase in Python
- Python First Character of String Uppercase
- Python Concatenate String and Variable (int, float, etc)
- How to Find Length of String in Python
- Python: String Join Function
- Python String Append Examples
- How to replace a character in a string in python
- Python – Count Number of Occurrences in String
- Python Remove Last Character from String
- Python Remove Character From String
- Python Program to Reverse String
- Python Program Count vowels in String
- Python Split a String into Array of Characters, Space
- Python Program to Swap Two Character of Given String
- Python Program String Find
- Python: Two Given Strings and Swap First Two Characters of Each String
- Print All Permutations of Given String in Python
- Python | Check Whether a String Contains Number or Letter
- Python: Convert String to Float & Float to String
- Python Program to Reverse String Using Stack
- How To Convert Python Int to String and String to Int
- Python Convert String to List and List to String