-
-
Save IndhumathyChelliah/c58dcbd87385ee3eb094d96a576bf6c2 to your computer and use it in GitHub Desktop.
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
#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