Dictionaries#

Amanda R. Kube Jotte

The next collection of data we will discuss is the dictionary. Like lists, dictionaries are built into Python and do not require you to load any additional libraries. Dictionaries are Python’s built-in mapping tool. They map values onto keys and as are composed of key:value pairs.

Creating Dictionaries#

An empty dictionary can be created using {} as follows:

dictionary = {}

Where lists are indexed using numbers (for example my_list[0:3]), dictionaries are indexed using their keys. Below, we create a dictionary named my_dict which has two keys which are both strings and two values, one of which is a float and the other a string.

value1 = 2.4
value2 = "value here"

my_dict = {
    'key1': value1,
    'key2': value2
}

my_dict
{'key1': 2.4, 'key2': 'value here'}

If we want to access a value in this dictionary, we use its key.

my_dict['key1']
2.4

Dictionary keys must be unique and immutable. They can be strings, numbers, or tuples. Lists and dictionaries (that can be changed) are not allowed as dictionary keys.

We can also create dictionaries from lists of key:value pairs presented as tuples using dict().

dict([(1,"Susanna"),(2,"Evelyn"),(3,"Amanda")])
{1: 'Susanna', 2: 'Evelyn', 3: 'Amanda'}

In the previous section, we learned about list comprehensions. We can use dictionary comprehensions to create dictionaries in a similar way. Instead of using [] as in list comprehensions, a dictionary comprehension using {}.

{k:k+10 for k in (1,2,3)}
{1: 11, 2: 12, 3: 13}

Dictionary of Dictionaries#

Though in the previous two examples the values were all strings or all integers. As we saw when creating my_dict, values in a dictionary do not need to be of the same data type. Values can include integers, floats, strings, lists, …, or even other dictionaries! One useful application of a dictionary is as an address book.

Here we create a dictionary of dictionaries. (What are the keys of the top level dictionary? What are the keys of the second-level dictionary?)

addresses = {
    "Bill": {'phone':3125551234, 'email': 'bill@internet.com'},
    "Mary": {'phone':3125559876, 'email': 'mary@web.net'},
    "Sue": {'phone':3125552468, 'email': 'sue@email.edu'}
}
addresses
{'Bill': {'phone': 3125551234, 'email': 'bill@internet.com'},
 'Mary': {'phone': 3125559876, 'email': 'mary@web.net'},
 'Sue': {'phone': 3125552468, 'email': 'sue@email.edu'}}

If I want to know Mary’s information, I can access it using the key Mary.

addresses['Mary']
{'phone': 3125559876, 'email': 'mary@web.net'}

This returns a dictionary of information for Mary. If I am only interested in a certain piece of information, for example her phone number, I can access it using the phone key.

addresses['Mary']['phone']
3125559876

This returns only her phone number. Perhaps we can’t remember which keys are in the address book. We can use list() to list all of the keys.

list(addresses)
['Bill', 'Mary', 'Sue']

We can also check if certain keys are in our dictionary.

'Sue' in addresses
True
'Anne' in addresses
False
'Eve' not in addresses
True

Changing Dictionaries#

We can also add elements to a dictionary…

addresses['Dan'] = {'phone':3125555678} # Not all values need to have the same data type or length
addresses
{'Bill': {'phone': 3125551234, 'email': 'bill@internet.com'},
 'Mary': {'phone': 3125559876, 'email': 'mary@web.net'},
 'Sue': {'phone': 3125552468, 'email': 'sue@email.edu'},
 'Dan': {'phone': 3125555678}}

…and delete elements from a dictionary using del.

del addresses['Mary']
addresses
{'Bill': {'phone': 3125551234, 'email': 'bill@internet.com'},
 'Sue': {'phone': 3125552468, 'email': 'sue@email.edu'},
 'Dan': {'phone': 3125555678}}

Keys are unique so the following code would not add a new key, but rather change the value of the key:

addresses['Dan'] = {'phone':3125555678,'email': 'none'}
addresses
{'Bill': {'phone': 3125551234, 'email': 'bill@internet.com'},
 'Sue': {'phone': 3125552468, 'email': 'sue@email.edu'},
 'Dan': {'phone': 3125555678, 'email': 'none'}}

A major difference between dictionaries and lists and arrays is that dictionaries do not preserve the order of the key:value pairs. There is no first element in a dictionary; there are the elements 'Bill', 'Sue', and 'Dan', not necessarily in that order. Your code cannot depend on the keys coming out in any specific order. If you need a consistent order, you must sort the keys yourself.

Next, we will discuss a collection of data that is different from both lists and dictionaries in that it must include only one data type and is not built into Python. This collection is known as an array.