List objects in Python have a sort() method that will sort the list alphanumerically, ascending, by default:
Example
Sort the list alphabetically:
mylist = ["Assam", "Asansol", "Banaras", "Delhi", "Odisha"]
mylist.sort()
After sorting the list would be like ["Asansol","Assam", "Banaras", "Delhi", "Odisha"]
Sort the list numerically:
mylist = [100, 50, 65, 82, 23]
mylist.sort()
After sorting the list would be like [23,50,65,82,100]
Sort Descending
To sort descending, use the keyword argument reverse = True in sort () function
Sort the list descending:
mylist = ["Assam", "Asansol", "Banaras", "Delhi", "Odisha"]
mylist.sort(reverse = True)
After sorting the list would be like ["Odisha","Delhi","Banaras", "Assam","Asansol" ]
Sort the list of numeric values in descending order
mylist = [100, 50, 65, 82, 23]
mylist.sort(reverse = True)
After sorting the list would be like [100,82,65,50,23]
No comments:
Post a Comment