- key
- len
key sort of strings
- Another example for using a key.
- To sort the list according to length
examples/lists/sort_key.py
animals = ['chicken', 'cow', 'snail', 'elephant'] print(animals) animals.sort() print(animals) animals.sort(key=len) print(animals) animals.sort(key=len, reverse=True) print(animals)
['chicken', 'cow', 'snail', 'elephant'] ['chicken', 'cow', 'elephant', 'snail'] ['cow', 'snail', 'chicken', 'elephant'] ['elephant', 'chicken', 'snail', 'cow']