Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created July 16, 2020 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/6cbee4066ac2b1e620c3fce9b0eefc19 to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/6cbee4066ac2b1e620c3fce9b0eefc19 to your computer and use it in GitHub Desktop.
def sort_key(e):
return e[1]
l1 = [(1, 2, 3), (2, 1, 3), (11, 4, 2), (9, 1, 3)]
# By default,sorted based on first element
print(sorted(l1))
# Output:[(1, 2, 3), (2, 1, 3), (9, 1, 3), (11, 4, 2)]
# Sorted based on second element,by specifying user defined function sort_key
print(sorted(l1, key=sort_key))
# Output:[(2, 1, 3), (9, 1, 3), (1, 2, 3), (11, 4, 2)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment