By Hemanta Sundaray on 2021-09-06
Python provides two functions for sorting the items in a list.
The sort method sorts the items in the list in place.
If the items in the list are numeric, they are sorted in ascending order by default. If they are strings, they are sorted in alphabetical order.
We can reverse the default sort order to descending by providing the argument reverse=True.
models = ['Ninja', 'Panigale', 'Africa Twin']
models.sort()
print(models)
# ['Africa Twin', 'Ninja', 'Panigale']
digits = [9, 7, 1, 8, 12, 8, 6]
digits.sort()
print(digits)
# [1, 6, 7, 8, 8, 9, 12]
The sorted() function returns a sorted copy of the list.
models = ['Ninja', 'Panigale', 'Africa Twin']
models_sorted = sorted(models) # sorted() creates a copy
print(models_sorted)
# ['Africa Twin', 'Ninja', 'Panigale']
print(models)
# ['Ninja', 'Panigale', 'Africa Twin']