In this article we will discuss how to sort a list of numbers in ascending and descending order using two different techniques.
list.sort[] vs sorted[]
lists sort[] function
In Python, list provides a member function sort[] that can sorts the calling list in place.
sorted[] Function
Its a built in function that accepts an iterable objects and a new sorted list from that iterable.
Lets use both to sort a list of numbers in ascending and descending Order
Advertisements
Suppose we have a list of numbers i.e.
Sorting the List in ascending Order using sorted[]
# Create a sorted copy of existing list newList = sorted[listOfNum]It will create a newList with sorted numbers in ascending order i.e.
newList [1, 2, 5, 11, 21, 23, 45, 45, 50, 67]
Whereas, existing list will not change
listOfNum [23, 45, 21, 45, 2, 5, 11, 50, 1, 67]
Sorting the List in ascending Order using list.sort[]
# Sort the List in Place listOfNum.sort[]It will sort the list itself.
listOfNum is now a List Sorted in Ascending Order
Sorting the List in Descending Order using sorted[]
# Create a sorted [Descending Order] copy of existing list newList = sorted[listOfNum, reverse=True]It will create a newList with sorted numbers in Descending order i.e.
newList [67, 50, 45, 45, 23, 21, 11, 5, 2, 1]
Whereas, existing list will not change
listOfNum [23, 45, 21, 45, 2, 5, 11, 50, 1, 67]
Sorting the List in Descending Order using list.sort[]
# Sort the List in Place [Descending Order] listOfNum.sort[reverse=True]It will sort the list itself.
listOfNum is now a List Sorted in Descending Order
Complete example is as follows,
def main[]: # List of numbers listOfNum = [23, 45, 21, 45, 2, 5, 11, 50, 1, 67] # print the List print["Initial List", listOfNum, sep='\n'] print["Sorting the List in ascending Order"] # Create a sorted copy of existing list newList = sorted[listOfNum] # print the List print["New List", newList, sep='\n'] # print the List print["Existing List", listOfNum, sep='\n'] # Sort the List in Place listOfNum.sort[] # print the List print["List Sorted in Ascending Order", listOfNum, sep='\n'] print["Sorting the List in Descending Order"] # Create a sorted copy of existing list newList = sorted[listOfNum, reverse=True] # print the List print["New List", newList, sep='\n'] # print the List print["Existing List", listOfNum, sep='\n'] # Sort the List in Place [Descending Order] listOfNum.sort[reverse=True] # print the List print["List Sorted in Descending Order", listOfNum, sep='\n'] if __name__ == "__main__": main[]
Output:
Initial List [23, 45, 21, 45, 2, 5, 11, 50, 1, 67] Sorting the List in ascending Order New List [1, 2, 5, 11, 21, 23, 45, 45, 50, 67] Existing List [23, 45, 21, 45, 2, 5, 11, 50, 1, 67] List Sorted in Ascending Order [1, 2, 5, 11, 21, 23, 45, 45, 50, 67] Sorting the List in Descending Order New List [67, 50, 45, 45, 23, 21, 11, 5, 2, 1] Existing List [1, 2, 5, 11, 21, 23, 45, 45, 50, 67] List Sorted in Descending Order [67, 50, 45, 45, 23, 21, 11, 5, 2, 1]
Advertisements