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)
Try it Yourself »Example
Sort the list by the length of the values:def myFunc(e):
return len(e)
cars = ['Ford', 'Mitsubishi', 'BMW', 'VW']
cars.sort(key=myFunc)
Try it Yourself »Example
Sort a list of dictionaries based on the "year" value of the dictionaries:def myFunc(e):
return e['year']
cars = [
{'car': 'Ford', 'year': 2005},
{'car': 'Mitsubishi', 'year': 2000},
{'car': 'BMW', 'year': 2019},
{'car': 'VW', 'year': 2011}
]
cars.sort(key=myFunc)
from Fruitty Blog https://ift.tt/2Xm6qxW
via IFTTT
No comments:
Post a Comment