There are many in-built methods available in the python Standard Library which is useful in dictionary operations. Below we will see the examples of most frequently used dictionary methods. Some examples of in-built methods are:
- keys(): The method keys() returns a list of all the available keys in the dictionary.
Example:
dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97}
print(dict.keys())
Output:
dict_keys(['Name', 'Rollno', 'Dept', 'Marks'])
- items(): This method returns a list of dictionary’s (key, value) as tuple.
Example:
dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97}
print(dict.items())
Output:
dict_items([('Name', 'Harry'), ('Rollno', 30), ('Dept', 'cse'), ('Marks', 97)])
- values(): This method returns list of dictionary dictionary’s values from the key value pairs.
Example:
dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97}
print(dict.values())
Output:
dict_values(['Harry', 30, 'cse', 97])