Python List sort() Method
Definition and Usage
The sort()
method sorts the list ascending
by default.
You can also make a function to decide the sorting criteria(s).
Syntax
list.sort(reverse=True|False, key=myFunc)
Parameter Values
Parameter | Description |
---|---|
reverse | Optional. reverse=True will sort the list descending. Default is reverse=False |
key | Optional. A function to specify the sorting criteria(s) |
More Examples
Example
Sort the list descending:
cars = ['Ford', 'BMW', 'Volvo']
cars.sort(reverse=True)
Run example »
Example
Sort the list by the length of the values:
# A function that returns the length of the value:
def myFunc(e):
return len(e)
cars = ['Ford', 'Mitsubishi', 'BMW', 'VW']
cars.sort(key=myFunc)
Run example »
Example
Sort the list by the length of the values and reversed:
# A function that returns the length of the value:
def myFunc(e):
return len(e)
cars = ['Ford', 'Mitsubishi', 'BMW', 'VW']
cars.sort(reverse=True, key=myFunc)
Run example »