Skip to content

Computer Science · Ch 10 — Tuples and Dictionaries

Dictionary Methods and Built-in functions

10.11

Dictionary Methods and Built-in functions

Python provides many built-in functions and methods for working with dictionaries. Table 10.2 of the textbook collects the commonly used ones; each is explained below with a worked example.

Method / FunctionWhat it does
len()Number of key: value pairs in the dictionary
dict()Builds a dictionary from a sequence of key-value pairs
keys()All the keys of the dictionary
values()All the values of the dictionary
items()All the (key, value) pairs as tuples
get()Value for a given key; None if the key is absent
update()Appends another dictionary's items into this one
delDeletes one item by key, or the whole dictionary
clear()Removes all the items, leaving an empty dictionary

len() — number of items

Returns the length, i.e. the number of key: value pairs of the dictionary passed as argument:

>>> marks = {'Anita': 95, 'Bala': 89, 'Chetan': 92, 'Divya': 85}
>>> len(marks)
4

dict() — build a dictionary from key-value pairs

Given a sequence of key-value pairs (for example a list of two-element tuples), dict() assembles them into a dictionary:

>>> pairs = [('Anita', 95), ('Bala', 89), ('Chetan', 92), ('Divya', 85)]
>>> pairs
[('Anita', 95), ('Bala', 89), ('Chetan', 92), ('Divya', 85)]
>>> marks = dict(pairs)
>>> marks
{'Anita': 95, 'Bala': 89, 'Chetan': 92, 'Divya': 85}

keys() — the keys of the dictionary

>>> marks.keys()
dict_keys(['Anita', 'Bala', 'Chetan', 'Divya'])

values() — the values of the dictionary

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

items() — the (key, value) pairs

Returns the items as a list of (key – value) tuple pairs — the same view the Method-2 traversal loop of Section 10.10 iterates over:

>>> marks.items()
dict_items([('Anita', 95), ('Bala', 89), ('Chetan', 92), ('Divya', 85)])

get() — lookup without KeyError

Returns the value corresponding to the key passed as argument. Crucially, if the key is not present, get() returns None instead of raising the KeyError that square-bracket access would:

>>> marks.get('Divya')
85
>>> marks.get('Farhan')      # key absent — returns None, no error
>>>

update() — merge another dictionary in

update() appends the key-value pairs of the dictionary passed as argument to the key-value pairs of the given dictionary. The argument dictionary itself is left unchanged:

>>> marks = {'Anita': 95, 'Bala': 89, 'Chetan': 92, 'Divya': 85}
>>> more  = {'Farhan': 79, 'Gita': 89}
>>> marks.update(more)
>>> marks
{'Anita': 95, 'Bala': 89, 'Chetan': 92, 'Divya': 85, 'Farhan': 79, 'Gita': 89}
>>> more
{'Farhan': 79, 'Gita': 89}

del — delete an item, or the whole dictionary

del dictionary[key] deletes the single item with the given key. Writing del dictionary_name (no key) removes the entire dictionary from memory — after that, even mentioning its name is a NameError:

Table 10.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}