Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created June 24, 2020 03:37
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/c58dcbd87385ee3eb094d96a576bf6c2 to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/c58dcbd87385ee3eb094d96a576bf6c2 to your computer and use it in GitHub Desktop.
#define function to take second element
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