Sorting Elements in a Copy of a List in Python
You can also sort the elements in a list using the sorted function. In its parameter, we specify the list. The function returns a copy of the original list with the order of the elements changed. If you specify the value reverse=True in the second optional parameter of the function, the elements will be sorted in descending order.
Let's sort the items in the list in ascending order:
lst = [3, 2, 1]
res = sorted(lst)
print(res) # [1, 2, 3]
Given a list:
lst = ['d', 'c', 'b', 'a']
Sort it in ascending order.
Given a list:
lst = [4, 12, 24]
Sort it in descending order.
Given a list:
lst = [10, 8, 6, 4]
Sort it in ascending order.