Dictionaries

What’s in a dictionary

Another key basic object in python is the dictionary. Dictionaries are iterables, but unlike lists (and tuples if you got that far) values in a dictionary are indexed by a user specified key rather than a positional argument. the default structure of a dictionary is {key: value}. The value can be any object in python (numbers, strings, boolean, lists, and so on). Keys are a bit more proscriptive; the rules are a bit more complex, but for beginners it’s normally enough to know that numbers and strings are able to be keys. For more details on exactly what python objects can be keys in a dictionary see: Why Lists Can’t be Dictionary Keys. Note as of python version 3.6 dictionaries will remember the order that the data was input, previously they were un-ordered. Now for some examples:

In [1]: my_dict = {'key': 'value'}

In [2]: my_dict
Out[2]: {'key': 'value'}

# to access data from a dictionary you use brackets and the key
In [3]: my_dict['key']
Out[3]: 'value'

# it is possible to assign new values to a dictionary
In [4]: my_dict['new_key'] = 'new_value'

In [5]: my_dict['new_key']
Out[5]: 'new_value'

# it is also possible to overwrite existing keys
In [6]: my_dict['key'] = 'peanut butter'

In [7]: my_dict['key']
Out[7]: 'peanut butter'

# you can also create dictionaries from nest lists (or tuples) of key, value pairs and the dict() function:
In [8]: temp = [
   ...:         ['peanut butter', 'jam'],
   ...:         ['eggs', 'bacon'],
   ...:         ['muslie', 'milk']
   ...:         ]
   ...: 

In [9]: my_food = dict(temp)

In [10]: my_food
Out[10]: {'peanut butter': 'jam', 'eggs': 'bacon', 'muslie': 'milk'}

In [11]: my_food['eggs']
Out[11]: 'bacon'

# just like lists dictionaries have length
In [12]: len(my_food)  # returns the number of key, value pairs
Out[12]: 3

# the keyword *in* also works, but only looks in the dictionaries keys
In [13]: 'eggs' in my_food
Out[13]: True

In [14]: 'bacon' in my_food
Out[14]: False

Important dictionary functions

Just like lists, dictionaries have a number of useful built in functions here we’ll showcase the four most important - .update(), .keys(), .values(), .items()

# .update adds the entries of a second dictionary to the first
In [15]: my_dict = {'Bert': 'Ernie'}

In [16]: another_dict = {'Sherlock Holmes': 'Watson', 'Batman': 'Robin'}

In [17]: my_dict.update(another_dict)

In [18]: my_dict
Out[18]: {'Bert': 'Ernie', 'Sherlock Holmes': 'Watson', 'Batman': 'Robin'}

# .keys(), .values(), .items() are all about accessing all of the keys, values,
# and (key, value) pairs in a dictionary, respectively.
# they are useful to see what's in your dictionary and for iteration which we'll talk about later
In [19]: my_dict.keys()
Out[19]: dict_keys(['Bert', 'Sherlock Holmes', 'Batman'])

In [20]: my_dict.values()
Out[20]: dict_values(['Ernie', 'Watson', 'Robin'])

In [21]: my_dict.items()
Out[21]: dict_items([('Bert', 'Ernie'), ('Sherlock Holmes', 'Watson'), ('Batman', 'Robin')])

# note that the behaviour of these functions are slightly different in python 2.7