Lists of number or string values can be sorted with the sort() method.
>>> spam = [2, 5, 3.14, 1, -7]
>>> spam.sort()
>>> spam
[-7, 1, 2, 3.14, 5]
You can also sort in reverse.
>>> spam.sort(reverse=True)
>>> spam
[5, 3.14, 2, 1, -7]
- Sorts the list in place
- Can’t sort mixed type lists. (i.e. numbers & strings)
- Uses ASCIIbetical order
ASCIIbetical sorting.
>>> spam = ['Alice', 'ants', 'Bob', 'badgers', 'Carol', 'cats']
>>> spam.sort()
>>> spam
['Alice', 'Bob', 'Carol', 'ants', 'badgers', 'cats']
Sort in regular alphabetical. Use key=str.lower
>>> spam = ['a', 'z', 'A', 'Z']
>>> spam.sort(key=str.lower)
>>> spam
['a', 'A', 'z', 'Z']
Causes sort to treat strings as all lowercase while sorting but without changing actual values.