Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

sorting with sorted

  • sorted
animals = ['chicken', 'cow', 'snail', 'elephant']
print(animals)         # ['chicken', 'cow', 'snail', 'elephant']

srd = sorted(animals)
print(srt)             # ['chicken', 'cow', 'elephant', 'snail']
print(animals)         # ['chicken', 'cow', 'snail', 'elephant']

rev = sorted(animals, reverse=True, key=len)
print(rev)             # ['elephant', 'chicken', 'snail', 'cow']
print(animals)         # ['chicken', 'cow', 'snail', 'elephant']