It is quite straightforward to apply a simple sorted()
function however, in this lesson we will check out more advanced applications with key and reverse parameters.
Although not introduced in .sort()
method’s lesson for learning reasons, key and reverse parameters also work in .sort()
method.
In general though sorted()
function is a generally more accepted practice since it doesn’t automatically change the original list and for this reason more prone to unintentional data accidents.
For the simple use, just type your list inside sorted()
function and your list will be sorted.
Unlike .sort()
method sorted()
function will not change your list’s order. Instead it will create a new sequence.
You can assign the result of the sorted()
function to a new variable or to the existing list if you’d like to replace it.
Let’s see an example.