Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created June 24, 2020 03:13
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/f5a75ec9eeeb5e364fe54b867da84449 to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/f5a75ec9eeeb5e364fe54b867da84449 to your computer and use it in GitHub Desktop.
#Sorting string
s2="hello"
print (sorted(s2))#Output:['e', 'h', 'l', 'l', 'o']
print (sorted(s2,reverse=True))#Output:['o', 'l', 'l', 'h', 'e']
#Sorting list
l1=[1,4,5,2,456,12]
print (sorted(l1)) #Output:[1, 2, 4, 5, 12, 456]
print (sorted(l1,reverse=True))#Output:[456, 12, 5, 4, 2, 1]
#Sorting tuple
t1=(15,3,5,7,9,11,42)
print(sorted(t1))#Output:[3, 5, 7, 9, 11, 15, 42]
print (sorted(t1,reverse=True))#Output:[42, 15, 11, 9, 7, 5, 3]
#Sorting List of tuples
t2=[(1,2),(11,12),(0,2),(3,2)]
print (sorted(t2))#Output:[(0, 2), (1, 2), (3, 2), (11, 12)]
print(sorted(t2,reverse=True))#Output:[(11, 12), (3, 2), (1, 2), (0, 2)]
#Sorting set
s1={1,4,3,6,2,8,11,32}
print (sorted(s1))#Ouput:[1, 2, 3, 4, 6, 8, 11, 32]
print (sorted(s1,reverse=True))#Output:[32, 11, 8, 6, 4, 3, 2, 1]
#Sorting Dictionary
d1={2:'red',1:'green',3:'blue'}
#Returns list of sorted keys
print (sorted(d1)) #Output:[1, 2, 3]
#Returns list of sorted values
print (sorted(d1.values()))#Output:['blue', 'green', 'red']
#Returns list of tuples containing keys and values.Sorted based on keys.
print (sorted(d1.items()))#Output:[(1, 'green'), (2, 'red'), (3, 'blue')]
print (sorted(d1,reverse=True))#Output:[3, 2, 1]
print (sorted(d1.values(),reverse=True))#Output:['red', 'green', 'blue']
print (sorted(d1.items(),reverse=True))#Output:[(3, 'blue'), (2, 'red'), (1, 'green')]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment