Lecture 11 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:
Simply eng2sp['one']='uno' is good enough to create an entry in the dictionary, with a key and a corresponding content.



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


You can add a set of data/values to a key:
Here is a tuple:


A tuple can be in a set parentheses: same results as above


Or a list:


Or even nested dictionaries:



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. Delete elements in a dictionary:
A summary for all the methods to delete items in a dictionary:


3. Looping the dictionaries

3.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.
'for c in s' will scan through all the letters in the string. 'if c not in d' check if the 'c' at this moment is already in the dictionary as a 'key'. Looping a dictionary is actually looping the key but not the content/value.


You can extract the data using the attribute '.get()' to take out the data in the dictionary.
It is equivalent to using h['a']


3.2 Print out each key and the corresponding value



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



3.4 Invert a dictionary


3.5 Use the embedded methods '.keys()' and '.valued()'.


4. 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:


5. Lists from Dictionaries:
The following methods/attributes of the dictionary, contains the English words and the corresponding German words:


6. Dictionaries from lists:


7. Merging existing dictionaries:


8. Summary:
Operators on Dictionaries:
Operator
Explanation
len(d)
returns the number of stored entries, i.e. the number of (key,value) pairs.
del d[k]
deletes the key k together with his value
k in d
True, if a key k exists in the dictionary d
k not in d
True, if a key k doesn't exist in the dictionary d




Tasks:

1. Design a dictionary that shows the frequency (the data) of all letters (the keys) in 'HoustonRockets'.
2. Invert the key and the data from the dictionary you created in Problem 1. (Refer to the example in section 2.4).
3. Define a function that carries a dictionary as the argument. The function will loop through all the entries in the dictionary and return the name of the most populus city:
city_population = {"New York City":8550405, "Los Angeles":3971883, "Toronto":2731571, "Chicago":2720546, "Houston":2296224, "Vancouver":631486, "Boston":667137}
4. Define a function that carries the dictionary like the one in Problem 3 and generates a new dictionary that have the keys of 'Huge City' and 'Medium City' as the only two categories/keys. Any city has more than 2.5 M people is considered as a 'Huge City', the other ones will be a 'Medium City'.