Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created Jul 10, 2020
Embed
What would you like to do?
#creating dictionary by using iterables
d=dict([('red',1),('blue',2),('green',3)])
print (d)#Output:{'red': 1, 'blue': 2, 'green': 3}
#Returns the length(number of keys)of dictionary
print (len(d))#Output:3
#Returns the list of keys in the dictionary
print(list(d))#Output:['red', 'blue', 'green']
#Returns the sorted list of keys in the dictionary(by default ascending order)
print (sorted(d))#Output:['blue', 'green', 'red']
#Returns the sorted list of keys in the dictionary in descending order.
print (sorted(d,reverse=True))#Output:['red', 'green', 'blue']
#Returns the reversed list of keys in the dictionary.Returns an iterator object.
print (reversed(d))#Output:<dict_reversekeyiterator object at 0x005DEB90>
#Iterator object is converted to list object
print (list(reversed(d)))#Output:['green', 'blue', 'red']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment