Major Important Dictionary methods in python
1.len()
Return the length of the the Dictionary.
dict1 ={"fruits": "mango","model": "Ravi","year":"1964"}
print(len(dict1))
2.str()
This method produce the printable string which represent dictionary.
dict1 ={"fruits": "mango","model": "Ravi","year":"1964"}
print("This is string : %s " % str(dict1))
3.type()
This method will return type of dictionary same as type method tells about datatype.
dict1 ={"fruits": "mango","model": "Ravi","year":"1964"}
print("Type is %s" % type(dict1))
4.cmp()
This method will return
0 if both the dictionary are equal, return 1 when dic1> dic2 and return -1 when dic1< dic2.
dict1 = {'Name': 'kavi', 'Age': 8};
dict2 = {'Name': 'shivam', 'Age': 29};
dict3 = {'Name': 'Ravi', 'Age': 29};
dict4 = {'Name': 'Amit', 'Age': 8};
print "Return Value : %d" % cmp (dict1, dict2)
print "Return Value : %d" % cmp (dict2, dict3)
print "Return Value : %d" % cmp (dict1, dict4)
5.clear()
This method will remove all the elements form the dictionary.
dict1 ={"fruits": "mango","model": "Ravi","year":"1964"}
dic1.clear()
print(dict1)
6.copy()
This method will create the copy of one dictionary to another dictionary.
dict1 ={"fruits": "mango","model": "Ravi","year":"1964"}
dict2=dict1.copy()
print(dict2)
7.items()
This method will return the object of view which contain the key value pair of dictionary.
dict1 ={"fruits": "mango","model": "Ravi","year":"1964"}
dict2=dict1.items()
dict1["year"] =2020
print(dict2)
