Lecture notes · Programming in Python · Lecture 14

Dictionaries

MIT 6.100L Intro to CS and Programming using Python, lecture 14, explained in plain English

Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.

Learn this lecture free

Watch or read the original lecture: MIT OpenCourseWare.

Part 1 · 0:00–10:00

This part explains why storing student data in separate lists gets messy, which sets up the need for a new kind of data structure called a dictionary.

Key ideas

Words to know

compound data type
A container that holds more than one piece of data at once, such as a list, a tuple, or a dictionary.
mutable
Able to be changed after it is made. Lists are mutable, so you can add, remove, or change items in them.
index
The position number of an item in a list. The first item is at index 0.
nested for loop
A loop that sits inside another loop, so the inner loop runs fully for each step of the outer loop.

Example

The lesson's own example: Ana's name is at index 0 in the name list, so her grade is at index 0 in the grade list. If Ana drops the class, you have to remove her from the name list and from the grade list, and from every other list you keep, or the lists will no longer line up.

3 checked practice questions for this part are in the app.

Part 2 · 10:00–20:00

This part introduces dictionaries, which store pairs of a key and a value so you can look up a value by its key.

Key ideas

Words to know

key
The custom label you use to find something in a dictionary, like a name or a word.
entry
One item in a dictionary, made of a key paired with its value.
immutable
Something that cannot be changed after it is made, such as a string or a number.
KeyError
The error Python shows when you try to look up a key that is not in the dictionary.

Example

Think of a phone contact list where each name is linked to a phone number. The name is the key, and the number is the value. If you look up a name that is not in your contacts, your phone shows an error, just like Python does with a KeyError.

3 checked practice questions for this part are in the app.

Part 3 · 20:00–30:00

This part shows how to look up, add, change, and delete entries in a dictionary, and how to check whether a key exists.

Key ideas

Words to know

key
The label you use to find a value in a dictionary, like a name that points to a grade.
mutate
To change something in place, so the original is changed instead of making a new copy.
iterate
To go through items one by one, usually with a loop.
del
A keyword that removes an entry from a dictionary or an item from a list.

Example

Think of a phone's contact list. The name is the key, and the phone number is the value. You look up a number by typing the name, you add a new name to the list, and you can erase a name, and each of these changes the same contact list.

3 checked practice questions for this part are in the app.

Part 4 · 30:00–40:00

This part shows how to look at every entry in a dictionary, and why you should never count on the entries being in any particular order.

Key ideas

Words to know

iterable
Something you can loop through one item at a time, such as a list or the result of keys().
tuple
A small group of values held together in order, written with parentheses, such as (key, value).
mutate
To change something in place. keys() and values() do not mutate the dictionary, so the dictionary stays the same after you call them.
cast
To convert one type of data into another, such as turning the result of keys() into a list.

Example

Think of a shelf of labeled boxes, where each label is a key and each box holds a value. You can read all the labels, look at all the box contents, or go through each box with its label together. Looking does not move or change any box, and the boxes are not necessarily lined up in a set order.

3 checked practice questions for this part are in the app.

Part 5 · 40:00–50:00

This part explains how to count matching keys and values in a dictionary, how copying and aliasing work with dictionaries, what kinds of keys and values are allowed, and why keys must be unchangeable.

Key ideas

Words to know

alias
A second name that points to the same object. If you change the object through one name, the change shows up through the other name too.
immutable
Something that cannot be changed after it is made. Numbers, strings, and tuples are examples in this lesson.
hashable
The lesson says keys must be hashable, which means Python can run a hash function on them. For this class, you can think of it as the same as immutable.
hash function
A function that takes in a key and turns it into a number. Python uses that number to decide where to store the value.

Example

Think of a coat check where each coat gets a numbered hook based on the name on the ticket. If the name on the ticket changed after you hung the coat, the attendant would look at the wrong hook and not find your coat. That is why a dictionary key must stay the same once it is stored.

3 checked practice questions for this part are in the app.

Part 6 · 50:00–1:00:00

This part explains why dictionary keys must be unchangeable, how to look up nested data in a dictionary, and how to collect values into one flat list.

Key ideas

Words to know

hash function
A rule that turns a key into a memory location, so the computer knows where to store or find the value for that key.
mutable
Able to be changed after it is created. A list is mutable, so its contents can change.
dictionary
A collection that stores pairs of a key and a value, so you can look up a value by its key, like finding a word's meaning in a real dictionary.
concatenate
To join two things end to end. With lists, plus joins the items of one list onto another.

Example

Think of a coat check where each coat gets a ticket number based on the name on the tag. If the name on the tag changes later, the attendant looks in the wrong spot and cannot find the coat. That is why a dictionary key must stay the same.

3 checked practice questions for this part are in the app.

Part 7 · 1:00:00–1:10:00

This part explains how dictionaries store data by key, and starts building a program that counts how often each word appears in a song.

Key ideas

Words to know

key
The label you use to look up something in a dictionary, like a word that points to its count.
hashable
A type of value that Python can use as a dictionary key. The lesson says keys cannot be immutable or hashable in the way it describes, so the rule matters when choosing keys.
split
A function that cuts a string into pieces. By default it cuts at each space, so a sentence becomes a list of words.
frequency dictionary
A dictionary that maps each word to the number of times it appears in the text.

Example

Imagine a party where every guest wears a name tag, and you want to count how many times each name shows up on the guest list. You keep a notebook with each name written down, and each time you see a name again, you add one tally next to it. That notebook is like a frequency dictionary: the names are the keys and the tallies are the values.

3 checked practice questions for this part are in the app.

Part 8 · 1:10:00–1:17:59

This part shows how to find the most frequent words in a frequency dictionary, then repeatedly remove those words to find the next most frequent group.

Key ideas

Words to know

mutation
Changing an object, such as a dictionary, in place instead of making a new copy.
tuple
A group of values held together in one object, written with commas and sometimes parentheses. Here, the function returns a tuple holding a list of words and a number.
del keyword
A Python word that removes an entry from a dictionary. Using it changes the original dictionary.
items
A dictionary tool that lets you loop through each key and value pair, one pair at a time.

Example

Think of a box of tally cards, where each card has a word and a count. To find the most common words, you pick out all the cards with the biggest count, set them aside, and then look again at the cards that are left. Each time you set aside a group, the next biggest count is the new top group.

3 checked practice questions for this part are in the app.

← Lecture 13: Exceptions and Assertions · Lecture 15: Recursion →

Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. MIT OpenCourseWare · CC BY-NC-SA 4.0 · not affiliated with or endorsed by MIT. These notes are shared under the same license.

LessonBound · Free lecture notes · Privacy · Terms · New York