Created
December 24, 2017 08:20
-
-
Save SteadBytes/f19eb0abdf7657ba90ece0add4ff6e73 to your computer and use it in GitHub Desktop.
Order by multiple values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lis = [(18, 4), (19, 4), (14, 3), (15, 3), (31, 3), (25,4)] | |
# sort by tuple[1], then tuple[0] | |
print(sorted(lis, key=lambda x: (x[1], x[0]))) | |
# >>> [(14, 3), (15, 3), (31, 3), (18, 4), (19, 4), (25, 4)] | |
# also use for max()/min() | |
print(max(lis, key=lambda x: (x[1], x[0]))) | |
# >>> (25, 4) | |
print(min(lis, key=lambda x: (x[1], x[0]))) | |
# >>> (14, 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment