# Dictionaries¶

## The Python Dictionary¶

Python supports a container type called a dictionary.

This is also known as an “associative array”, “map” or “hash” in other languages.

In a list, we use a number to look up an element:

names = "Martin Luther King".split(" ")

names[1]

'Luther'


In a dictionary, we look up an element using another object of our choice:

me = {"name": "James", "age": 39, "Jobs": ["Programmer", "Teacher"]}

me

{'name': 'James', 'age': 39, 'Jobs': ['Programmer', 'Teacher']}

me["Jobs"]

['Programmer', 'Teacher']

me["age"]

39

type(me)

dict


### Keys and Values¶

The things we can use to look up with are called keys:

me.keys()

dict_keys(['name', 'age', 'Jobs'])


The things we can look up are called values:

me.values()

dict_values(['James', 39, ['Programmer', 'Teacher']])


When we test for containment on a dict we test on the keys:

"Jobs" in me

True

"James" in me

False

"James" in me.values()

True


### Immutable Keys Only¶

The way in which dictionaries work is one of the coolest things in computer science: the “hash table”. The details of this are beyond the scope of this course, but we will consider some aspects in the section on performance programming.

One consequence of this implementation is that you can only use immutable things as keys.

good_match = {
("Lamb", "Mint"): True,
("Bacon", "Chocolate"): False
}


but:

illegal = {
["Lamb", "Mint"]: True,
["Bacon", "Chocolate"]: False
}

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [14], in <cell line: 2>()
----> 1 illegal = {
2     ["Lamb", "Mint"]: True,
3     ["Bacon", "Chocolate"]: False
4 }

TypeError: unhashable type: 'list'


Remember – square brackets denote lists, round brackets denote tuples.

### No guarantee of order¶

Another consequence of the way dictionaries work is that there’s no guaranteed order among the elements:

my_dict = {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4}
print(my_dict)
print(my_dict.values())

{'0': 0, '1': 1, '2': 2, '3': 3, '4': 4}
dict_values([0, 1, 2, 3, 4])


## Sets¶

A set is a list which cannot contain the same element twice. We make one by calling set() on any sequence, e.g. a list or string.

name = "James Hetherington"
unique_letters = set(name)

unique_letters

{' ', 'H', 'J', 'a', 'e', 'g', 'h', 'i', 'm', 'n', 'o', 'r', 's', 't'}


Or by defining a literal like a dictionary, but without the colons:

primes_below_ten = {2, 3, 5, 7}

type(unique_letters)

set

type(primes_below_ten)

set

unique_letters

{' ', 'H', 'J', 'a', 'e', 'g', 'h', 'i', 'm', 'n', 'o', 'r', 's', 't'}


This will be easier to read if we turn the set of letters back into a string, with join:

"".join(unique_letters)

'hJainoeg stHrm'


A set has no particular order, but is really useful for checking or storing unique values.

Set operations work as in mathematics:

x = set("Hello")
y = set("Goodbye")

x & y  # Intersection

{'e', 'o'}

x | y  # Union

{'G', 'H', 'b', 'd', 'e', 'l', 'o', 'y'}

y - x  # y intersection with complement of x: letters in Goodbye but not in Hello

{'G', 'b', 'd', 'y'}


Your programs will be faster and more readable if you use the appropriate container type for your data’s meaning. Always use a set for lists which can’t in principle contain the same data twice, always use a dictionary for anything which feels like a mapping from keys to values.