Skip to content

Informatics Practices · Ch 4 — Working with Lists and Dictionaries

Dictionary Methods and Built-in Functions

4.8

Dictionary Methods and Built-in Functions

Python supplies a set of ready-made functions and methods for working on dictionaries, so that everyday jobs — counting the pairs, pulling out all the keys or all the values, fetching a value safely, merging two dictionaries, or emptying one — never have to be coded by hand. The commonly used ones are collected in the book's Table 4.2, and each is examined below with the dictionary dict1 = {'Mohan':95, 'Ram':89, 'Suhel':92, 'Sangeeta':85} as the running example.

Method / FunctionWhat it does
len()Returns the number of key-value pairs in the dictionary
dict()Builds a dictionary from a sequence of key-value pairs
keys()Returns all the keys of the dictionary
values()Returns all the values of the dictionary
items()Returns the key-value pairs as tuples
get()Returns the value for a given key; gives None if the key is absent
update()Appends the pairs of another dictionary into this one
clear()Deletes all the items, leaving an empty dictionary
delDeletes the item with a given key, or the whole dictionary

len()

len() takes the dictionary as its argument and returns its length — the count of key-value pairs it contains:

>>> dict1 = {'Mohan':95, 'Ram':89, 'Suhel':92, 'Sangeeta':85}
>>> len(dict1)
4

dict()

dict() creates a dictionary out of a sequence of key-value pairs. If the pairs are held as tuples inside a list, passing that list to dict() converts the whole thing into a dictionary in one step:

>>> pair1 = [('Mohan',95), ('Ram',89), ('Suhel',92), ('Sangeeta',85)]
>>> dict1 = dict(pair1)
>>> dict1
{'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85}

keys()

keys() returns all the keys present in the dictionary:

>>> dict1.keys()
dict_keys(['Mohan', 'Ram', 'Suhel', 'Sangeeta'])

values()

values() is the companion of keys() — it returns all the values:

>>> dict1.values()
dict_values([95, 89, 92, 85])

items()

items() returns the key-value pairs of the dictionary, each pair packaged as a tuple. This is the same method that powers the two-variable traversal loop of the previous section:

>>> dict1.items()
dict_items([('Mohan', 95), ('Ram', 89), ('Suhel', 92), ('Sangeeta', 85)])

get()

get() looks up the key passed as its argument and returns the corresponding value. Its special property shows when the key is not present: instead of stopping the program with an error (as plain indexing with square brackets would), it quietly returns None:

>>> dict1.get('Sangeeta')
85
>>> dict1.get('Sohan')
>>>

Nothing is displayed for 'Sohan' because the returned value is None — the key does not exist in the dictionary.

update()

update() appends the key-value pairs of the dictionary given as its argument into the dictionary on which it is called. The argument dictionary itself is left unchanged:

>>> dict1 = {'Mohan':95, 'Ram':89, 'Suhel':92, 'Sangeeta':85}
>>> dict2 = {'Sohan':79, 'Geeta':89}
>>> dict1.update(dict2)
>>> dict1
{'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85, 'Sohan': 79, 'Geeta': 89}
>>> dict2
{'Sohan': 79, 'Geeta': 89}

After the call, dict1 carries all six pairs, while dict2 still holds only its original two.

clear()

clear() deletes all the items of the dictionary at once. The dictionary itself survives, but as an empty one:

>>> dict1 = {'Mohan':95, 'Ram':89, 'Suhel':92, 'Sangeeta':85}
>>> dict1.clear()
>>> dict1
{}

del

del works at two levels, and the difference matters.

Written with a key — del dictionary[key] — it deletes just that one item from the dictionary:

>>> dict1 = {'Mohan':95, 'Ram':89, 'Suhel':92, 'Sangeeta':85}
>>> del dict1['Ram']
>>> dict1 …
Table 4.2Built-in functions and methods for dictionary
MethodDescriptionExample
len()Returns the length or number of key: value pairs of the dictionary passed as the argument>>> dict1 = {'Mohan':95,'Ram':89, 'Suhel':92, 'Sangeeta':85}
>>> len(dict1)
4
dict()Creates a dictionary from a sequence of key-value pairspair1 = [('Mohan',95),('Ram',89), ('Suhel',92),('Sangeeta',85)]
>>> pair1
[('Mohan', 95), ('Ram', 89), ('Suhel', 92), ('Sangeeta', 85)]
>>> dict1 = dict(pair1)
>>> dict1
{'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85}
keys()Returns a list of keys in the dictionary>>> dict1 = {'Mohan':95, 'Ram':89, 'Suhel':92, 'Sangeeta':85}
>>> dict1.keys()
dict_keys(['Mohan', 'Ram', 'Suhel', 'Sangeeta'])
values()Returns a list of values in the dictionary>>> dict1 = {'Mohan':95, 'Ram':89, 'Suhel':92, 'Sangeeta':85}
>>> dict1.values()
dict_values([95, 89, 92, 85])
items()Returns a list of tuples (key — value) pair>>> dict1 = {'Mohan':95, 'Ram':89, 'Suhel':92, 'Sangeeta':85}
>>> dict1.items()
dict_items([( 'Mohan', 95), ('Ram', 89), ('Suhel', 92), ('Sangeeta', 85)])
get()Returns the value corresponding to the key passed as the argument

If the key is not present in the dictionary it will return None
>>> dict1 = {'Mohan':95, 'Ram':89, 'Suhel':92, 'Sangeeta':85}
>>> dict1.get('Sangeeta')
85
>>> dict1.get('Sohan')
>>>
update()appends the key-value pair of the dictionary passed as the argument to the key-value pair of the given dictionary>>> dict1 = {'Mohan':95, 'Ram':89, 'Suhel':92, 'Sangeeta':85}
>>> dict2 = {'Sohan':79,'Geeta':89}
>>> dict1.update(dict2)
>>> dict1
{'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85, 'Sohan': 79, 'Geeta': 89}
>>> dict2
{'Sohan': 79, 'Geeta': 89}