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
#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