Title: 7
Course: Start programming with Python in 10 steps

Grow your dictionaries

Once you find instances of words or phrases in large texts, you will quickly see that printing the found strings on the screen or in a file is not the most useful thing to do. Most of the time, you will want to store the matched strings somewhere in the RAM memory for further processing within your program. For this, you can use the variables that we already know rather well (literals and lists), but another type of a variable is, in fact, much more handy: dictionaries. At this point, we are already talking about data structures because dictionaries are boxes where you can keep really complex data. They’re not the only data structure you will see in Python (as a matter of fact, lists are a data structure too!), but mastering dictionaries is worth some special attention.

👉    6 things to know at this step

1. Dictionaries are a more general variable type than lists. Both lists and dictionaries store data as key:value pairs. The difference is that the keys in lists are always ordered integers starting from 0 (indices), while keys in dictionaries are strings.
2. Any string can be a key in a dictionary as long as it is unique. If you try to assign two different values for the same key, only one value will be stored (the one that you assigned the last). Remember this if you find yourself looking for a missing piece of information in your dictionary.
3. A value in a dictionary can be a literal, another dictionary or a list. In this sense, dictionaries and lists are the same (values of lists can also be other lists and dictionaries), just that it was too early to talk about that before.
4. There are several ways how you can iterate through dictionaries and update the values, e.g. by keys or by values. Now that we say that, the same is true for lists too, just that the difference is more subtle.
5. The most common use of dictionaries in text processing is creating frequency lists, for which you need to store and update many count variables.
6. Dictionaries used to be unordered (every time you print them, you get a different order of elements) and much faster for look-up than lists for a long time. This is not the case any more (since the Python version 3.7).

🧑‍🎓    Learn actively

📝   Read about dictionaries in the W3Schools tutorial. The first page should already give you a good idea of 1️⃣ and 2️⃣, but we’re going to check everything under the item Python dictionaries.

▶️   First, read and try out the examples in the Access Items, Change Items, Add Items and Remove Items parts. These are the most common operations on dictionaries, the ones that you will need almost at all times when working with dictionaries. For complimentary explanatations, here is a video example by Martin Volk:

📹   Building a Python dictionary by Martin Volk

▶️   Then, the Loop Dictionaries part will show you examples of 4️⃣. Check the video below for a practical example:

📹   Iterating over a dictionary by Martin Volk

▶️   Complete the Copy Dictionaries part, and check Nested Dictionaries to see examples of 3️⃣ too.

📝   You might want to keep a bookmark to W3school’s list of Dictionary Methods for further reference. Click on any method’s name if you need a reminder of what it does or usage examples. You can also complete their short dictionary exercises to make sure you’re at ease with the basics.

❗  Dictionaries are very easy to understand passively, but working with them requires quite some thinking and connecting the dots. Probably one of the most frequent errors returned by Python is about undefined keys in dictionaries. As a beginner, you are likely to see this error a lot because you will find yourself trying to update a dictionary item that does not exist. Understanding why a key does not exist might take some effort, but this is your way to mastery!

▶️   Finally, make sure to watch these two more videos by Martin Volk. They will highlight the differences between lists and dictionaries (which you might already be wondering about!) and give you a few examples of what dictionaries can be used for when working with language.

📹   Differences between dictionaries and lists by Martin Volk

📹   Typical uses of dictionaries for linguists by Martin Volk

❓ Master dictionaries by connecting the dots

1.  Frequency distribution

Taking as input the same file with which we worked in the previous steps, write two programs:

1.1  CharF.py outputs (prints on the screen) for each letter how many times it appeared in the file, that is, the frequency distribution over the characters. For this, you build a dictionary whose keys are characters. What are the values of your dictionary?

1.2  WordF.py outputs (prints on the screen) for each word how many times it appeared in the file, that is, the frequency distribution of the words. Here, your keys are words.

Hint 1: To solve this task, both of your programs need two loops, one for growing a dictionary and the other for printing it.

Hint 2: To grow your dictionary, you will need to update the values of your keys.

Hint 3: It might sound trivial, but you cannot update the value of a key if you didn't previously assign a value to this key. The method get() will help you assign default values before you update them in your loop.

2. Language teacher

Imagine you want to help your friend learn German. Your friend needs to learn the words and phrases contained in this file (one word or phrase per line). You write a program that presents to your friend the target words (or phrases) one by one with a request to type in their English translation. Once the translation is typed in, your program checks if it is correct. If it is correct, it awards 5 points and outputs a message to your friend about it. If not, it asks your friend to try again. Once your friend ends a session, your program outputs how many points your friend earned and how many could be maximally earned. To solve this task, you will also need this file.

Hint 1: Use a dictionary for checking if your friend's answer is correct.

Hint 2: You might want to allow your friend to pass an item after a certain number of trials.

Hint 3: Your friend needs to be able to end a session.

Remember: