Skip to content

Instantly share code, notes, and snippets.

@SteadBytes
Created December 24, 2017 08:20
Show Gist options
  • Save SteadBytes/f19eb0abdf7657ba90ece0add4ff6e73 to your computer and use it in GitHub Desktop.
Save SteadBytes/f19eb0abdf7657ba90ece0add4ff6e73 to your computer and use it in GitHub Desktop.
Order by multiple values
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