Lecture 13 Dictionaries

1. Introduction
Dictionaries are one of Python’s best features; they are the building blocks of many efficient and elegant algorithms.
A dictionary is like a list, but more general. In a list, the indices have to be integers; in a dictionary they can be (almost) any type. A dictionary contains a collection of indices, which are called keys, and a collection of values. Each key is associated with a single value. The association of a key and a value is called a key-value pair or sometimes an item. In mathematical language, a dictionary represents a mapping from keys to values, so you can also say that each key “maps to” a value. As an example, we’ll build a dictionary that maps from English to Spanish words, so the keys and the values are all strings.

The function dict creates a new dictionary with no items. Because dict is the name of a built-in function, you should avoid using it as a variable name.

The squiggly-brackets, {}, represent an empty dictionary. To add items to the dictionary, you can use square brackets:

This output format is also an input format. For example, you can create a new dictionary with three items:

In general, the order of items in a dictionary is unpredictable. But that’s not a problem because the elements of a dictionary are never indexed with integer indices. Instead, you use the keys to look up the corresponding values:

The key 'two' always maps to the value 'dos' so the order of the items doesn’t matter.

The in operator works on dictionaries, too; it tells you whether something appears as a key in the dictionary (appearing as a value is not good enough).

2. Looping the dictionaries

2.1. Count the frequency of all the letters in a word from the user's input.

Please keep in mind that the order does not matter in a dictionary, the index is the 'key' instead of the the real position in the dictionary.

You can extract the data using the attribute '.get()' to take out the data in the dictionary.

2.2 Print out each key and the corresponding value

2.3 Here is a function that takes a value and returns the first key that maps to that value

2.4 Invert a dictionary

3. Global variables

Global variables can be accessed from any function. Unlike local variables, which disappear when their function ends, global variables persist from one function call to the next.

If you try to change the variable locally (inside the function), it won't be changed globally. The following example shows you that it won't work when you try to print a 'local variable' outside of a function.

You can put the 'print' function inside the 'example2()' function, and it works.

One more example that tries to update a global variable:

The function does not pass any parameters inside it so the function cannot find what does 'count' mean. You can fix it as:
Tell Python the 'count' variable is the global 'count'.

Or, you can pass it to the inside function by:


1. Design a dictionary that shows the frequency (the data) of all letters (the keys) in 'HoustonRockets'.
2. Look at the snapshot of part of the team roster below. Make a dictionary that has the numbers as keys and the players' names as the data.

3. Invert the key and the data in Problem 2.
4. If the numbers are the keys, names are the data, change the player under No. 1 to 'Tracy McGrady'. (Mute it but do not mannually make up the new dictionary)