Build-in Dictionary Methods
Through out the examples, d will be used as a dictionary variable to show how to use built-in python dictionary methods.
d.keys()
This method returns the keys in a d. For example:
>>> d = {'name': 'Jon', 'age': 23, 'school': 'UCLA'}
>>> d.keys()
dict_keys(['name', 'age', 'school'])
d.values()
This method returns the values in a d. For example:
>>> d = {'name': 'Jon', 'age': 23, 'school': 'UCLA'}
>>> d.values()
dict_values(['Jon', 23, 'UCLA'])
d.items()
This built-in method returns a list of tuples containing the key-value pairs in d. Example:
>>> d = {'name': 'Jon', 'age': 23, 'school': 'UCLA'}
>>> d.items()
dict_items([('name', 'Jon'), ('age', 23), ('school', 'UCLA')])
Returned items are called view objects. For practicality, you can think of returning lists but you need to convert d.items() to list of keys and values. Example, let’s get the age’s value:
>>> d = {'name': 'Jon', 'age': 23, 'school': 'UCLA'}
>>> list(d.items())
[('name', 'Jon'), ('age', 23), ('school', 'UCLA')]
>>> list(d.items())[1][1]
23
d.get( key [, default])
This method returns the value for a given key. if the key is not found then it will return None. For example:
>>> d = {'name': 'Jon', 'age': 23, 'school': 'UCLA'}
>>> print(d.get('age'))
23
>>> print(d.get('lastname'))
None
>>> print(d.get('lastname', 'd does not have lastname'))
'd does not have lastname'
As you see in last print, you are able to assign a default value if the key does not exist.
d.clear()
This method empties dictionary d of all key-value pairs:
>>> d = {'name': 'Jon', 'age': 23, 'school': 'UCLA'}
>>> d
{'name': 'Jon', 'age': 23, 'school': 'UCLA'}
>>> d.clear()
{}
d.pop( key [, default])
This method removes a key from a dictionary and returns its associated value:
>>> d = {'name': 'Jon', 'age': 23, 'school': 'UCLA'}
>>> d.pop('age')
23
>>> d
{'name': 'Jon', 'school': 'UCLA'}
If key is not in dictionary d then it raises a KeyError exception. You can give a default value as well if key is not in d:
>>> d = {'name': 'Jon', 'age': 23, 'school': 'UCLA'}
>>> d.pop('lastname')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'lastname'
>>> d.pop('lastname', 'no lastname')
'no lastname'
d.popitem()
This method removes the last key-value from a dictionary and returns tuple:
>>> d = {'name': 'Jon', 'age': 23, 'school': 'UCLA'}
>>> d.popitem()
('school', 'UCLA')
>>> d
{'name': 'Jon', 'age': 23}
If the dictionary has no item in it then it will raise a KeyError exception:
>>> d = {}
>>> d.popitem()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'popitem(): dictionary is empty'
d.update(obj)
This method merges values of a dictionary d from obj (if obj is a dictionary). Lets demonstrate this method with examples to understand better:
>>> d1 = {'name': 'Jon', 'age': 23, 'school': 'UCLA'}
>>> d2 = {'name': 'Alp', 'lastname': 'Eren'}
>>> d1.update(d2)
>>> d1
{'name': 'Alp', 'age': 23, 'school': 'UCLA', 'lastname': 'Eren'}
See also: