Skip to content

Instantly share code, notes, and snippets.

@reikoNeko
Last active August 24, 2017 15:43
Show Gist options
  • Save reikoNeko/da117c1f3e25b074f7eade3e4f32f585 to your computer and use it in GitHub Desktop.
Save reikoNeko/da117c1f3e25b074f7eade3e4f32f585 to your computer and use it in GitHub Desktop.
lambda tuple sort
In [20]: foo = [(3,8), (5,3), (4,6), (1,5), (4,3), (4,7)]
In [21]: sorted(foo)
Out[21]: [(1, 5), (3, 8), (4, 3), (4, 6), (4, 7), (5, 3)]
In [22]: sorted(foo, key=lambda _: (-_[0],_[1]))
Out[22]: [(5, 3), (4, 3), (4, 6), (4, 7), (3, 8), (1, 5)]
In [23]: sorted(foo, key=lambda X: (-X[0], X[1]))
Out[23]: [(5, 3), (4, 3), (4, 6), (4, 7), (3, 8), (1, 5)]
# No magic difference between _ and X, they're just scope variables.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment