A dictionary is a more general version of a list because the index does not necessarily have to be an integer. A dictionary is defined by a set of case-sensitive unordered items (pairs of keys and values) where there is a mapping between a given key and its corresponding value. There is no order among the items in a dictionary. To define a new dictionary, use the Python built-in function dict()
or empty curly braces:
names = dict()
names = {}
print names
{}
The second line will return an empty set of curly brackets representing an empty dictionary.
The function len()
can be used to calculate the number items (i.e. key-value pairs) in a dictionary.
len(names)
0
Note about dictionary keys: Though the keys (or indices for a dictionary) can be values other than integers, the keys must be immutable. Keys can be strings, integers or tuples which is discussed in the next section. Items such as lists (which are mutable) cannot be used as keys in a dictionary, but lists can be values in a dictionary. The reason is because a dictionary is implemented using a hashtable and the keys must be hashable.
Note about dictionary values: Dictionaries values can be any type (strings, integers, other objects, or even other dictionaries).
The operators +
and *
do not work on dictionaries.
To add an item (key and value) to the dictionary, you can do one of the following ways:
[]
{}
names['JK'] = 'Rowling'
names = {'JK':'Rowling', 'Mark':'Twain', 'William':'Shakespeare', \
'George':'Orwell', 'JRR':'Tolkien', 'Emily':'Dickinson', 'Lewis':'Carroll'}
In the second line, you can multiple items with commas separating the key and values where here the key is the first name 'John' and the value is the last name 'Smith'. If you want to lookup a value given a dictionary and a key, you can use the bracket operator on a key to return the value. Here we are asking to print the last name of the author named Emily
.
print names['Emily']
Dickinson
names
{'Emily': 'Dickinson', 'George': 'Orwell', 'JK': 'Rowling', 'JRR': 'Tolkien', 'Lewis': 'Carroll', 'Mark': 'Twain', 'William': 'Shakespeare'}
Note: You cannot have duplicate keys in a dictionary. Assigning a value to an existing key will over-write the old value.
Note: You cannot reverse lookup a key (k) given a dictionary (d) and a value (v). There may be more than one key that maps to a given value, so you may have a list of keys that map to a given value.
def reverse_lookup(d, v):
for k in d:
if d[k] == v:
return k
raise ValueError, 'value does not appear in the dictionary'
print reverse_lookup(names, 'Dickinson')
Emily
The last line uses the statement raise
which can take in a detailed error message. Here, if the value does not exist in the dictionary, then we raise
an error message.
If you want to delete a specific item in a dictionary, you can use the del
function
del names['Mark']
print names
{'Lewis': 'Carroll', 'William': 'Shakespeare', 'JRR': 'Tolkien', 'JK': 'Rowling', 'George': 'Orwell', 'Emily': 'Dickinson'}
Similar to strings and lists, the in
(and not in
) operator works with dictionaries. The operator will if a key exists in a dictionary and return a True
or False
.
'Lewis' in names
True
To check if a value exists in a dictionary, you can use the dictionary method values()
printvals = names.values()
'Lewis' in printvals
False
Similar to strings, there are set of dictionary methods in Python that are useful to manipulate dictionaries. The syntax is the name of the dictionary followed by a dot (or period) followed by the name of the dictionary method.
keys()
= returns all the keys as a list from a dictionaryvalues()
= returns all the values as a list from a dictionaryitems()
= returns the list of items in the diction as a list of tuplesget()
= takes in a key and a default value as arguments and will return the value in the dictionary if the key exists or the second argument if the key does not exist. For example, the key-value pair Jane
-Austen
does not exisit in the names
dictionary, but JRR
does.print names.get('Jane', True)
print names.get('JRR', True)
True Tolkien
update()
= takes in a list of tuples and adds them to the dictionary as key-value pairs to an existing dictionaryclear()
= deletes all items from a dictionarynames.clear()
print names
{}
To traverse through a set of keys in a dictionary, you can use for
loops (similar to strings and lists):
names = {'JK':'Rowling', 'Mark':'Twain', 'William':'Shakespeare', \
'George':'Orwell', 'JRR':'Tolkien', 'Emily':'Dickinson', 'Lewis':'Carroll'}
for keys in names:
print names[keys]
Carroll Twain Shakespeare Tolkien Rowling Orwell Dickinson
A for
loop over an empty dictionary does not execute anything.
Another way to loop through the items in a dictionary is to use the dictionary method items()
which lists all the key-value pairs in the dictionary. Here we assign a name to the keys and a names to the values and then print each parameter.
for key, val in names.items():
print key, val
Lewis Carroll Mark Twain William Shakespeare JRR Tolkien JK Rowling George Orwell Emily Dickinson
print "keys: ", names.keys()
print "values: ", names.values()
names.items()
keys: ['Lewis', 'Mark', 'William', 'JRR', 'JK', 'George', 'Emily'] values: ['Carroll', 'Twain', 'Shakespeare', 'Tolkien', 'Rowling', 'Orwell', 'Dickinson']
[('Lewis', 'Carroll'), ('Mark', 'Twain'), ('William', 'Shakespeare'), ('JRR', 'Tolkien'), ('JK', 'Rowling'), ('George', 'Orwell'), ('Emily', 'Dickinson')]