Skip to content

Instantly share code, notes, and snippets.

@Nagasaki45
Last active August 29, 2015 14:00
Show Gist options
  • Save Nagasaki45/11400674 to your computer and use it in GitHub Desktop.
Save Nagasaki45/11400674 to your computer and use it in GitHub Desktop.

List in-place operations

method examples
sort

l = [2, 4, 1]

l.sort() # -> None!

l # -> [1, 2, 4]

l = [('me', 4), ('you', 2)]

l.sort(key=lambda x: x[1]) # -> None!

l # -> [('you', 2), ('me', 4)]

reverse

l = [2, 4, 1, 3]

l.reverse() # -> None!

l # -> [3, 1, 4, 2]

sort vs sorted

In addition to the list method sort there is the built-in function sorted. It accepts a list and return a new, sorted list!. The original list is untouched.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment