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()

list’s sort() function

In Python, list provides a member function sort() that can sorts the calling list in place.

sorted() Function

It’s a built in function that accepts an iterable objects and a new sorted list from that iterable.

Let’s use both to sort a list of numbers in ascending and descending Order

Suppose we have a list of number’s i.e.

Sorting the List in ascending Order using sorted()

It will create a newList with sorted numbers in ascending order i.e.

Whereas, existing list will not change

Sorting the List in ascending Order using list.sort()

It will sort the list itself.

listOfNum is now a  List Sorted in Ascending Order

Sorting the List in Descending Order using sorted()

It will create a newList with sorted numbers in Descending order i.e.

Whereas, existing list will not change

Sorting the List in Descending Order using list.sort()

It will sort the list itself.

listOfNum is now a  List Sorted in Descending Order

Complete example is as follows,

Output:

 

 

Click Here to Subscribe for more Articles / Tutorials like this.