Lecture 16: Dictionaries (with bonus Lambda and Map!)
Watch on YouTube →
Overview
Professor Rosen builds from character-frequency counting in Project Gutenberg’s Treasure Island file to show why dictionaries are a better fit than fixed-size lists for tracking arbitrary keys and values. He demonstrates dictionary-based character and word counts, discusses normalization and sorting, then introduces functions as values, lambda expressions, and map; course announcements also cover lab deadlines and the final project.
Key takeaways
- Dictionaries make frequency counting independent of a fixed character range: each observed character or word becomes a key, and its count becomes the associated value.
- A dictionary lookup is directional: keys retrieve values, keys must be unique, and multiple keys can share the same value.
- For word counts, split() handles whitespace separation and lower() merges capitalization variants, but punctuation still needs separate normalization.
- Python’s sorted() can order dictionary keys by their associated values by using the dictionary’s get method as the key function.
- Python functions can be stored and passed as values; lambda creates an inline function, while map applies that function to every item in an iterable.
Chapters
- Labs 4 and 5 are due Monday; Professor Rosen warns that students far behind may struggle to catch up even if late work is accepted.
- Dictionaries are the day’s main new Python topic; regular expressions and Monte Carlo approximation are among the upcoming subjects.
- The final project is expected to begin around November 5, with groups of three or fewer and the option to work individually.
- The example uses Project Gutenberg’s Treasure Island text file, identified as pg120.txt.
- On Windows, opening the text may require specifying encoding="UTF-8"; the file is read into lines with readlines().
- The initial question is how to identify the most frequent character, with spaces expected to outnumber letters.
- One proposed method assigns a separate variable to each letter from A to Z, requiring 26 counters and many conditional branches.
- A 26-element list improves storage by mapping A through Z to indexes 0 through 25.
- The list still requires repeated if-statements to decide which letter’s count to increment.
- Python’s ord() converts lowercase ASCII letters to numeric codes, so subtracting ord('a') maps a through z to indexes 0 through 25.
- An if-condition checks whether a character falls within the ASCII letter range before updating its list counter.
- enumerate() supplies both each list index and its value, while chr(index + ord('a')) converts an index back into a printable letter.
- The character-counting example expands to 128 counters for ASCII code points 0 through 127 and uses ord(character) as the counter index.
- The output includes non-letter characters such as spaces, commas, tabs, and newlines; spaces are especially frequent.
- ASCII-sized arrays do not handle arbitrary UTF-8 text naturally, motivating a data structure that can use words and other values as keys.
- A dictionary is created with curly braces and stores key-value associations; other languages may call the structure a map, hash map, or associative array.
- Example entries map integer keys 0 and 1 to "apple" and "banana", and key 100 to "durian".
- Retrieval uses the key, so D[0] returns "apple"; trying D["apple"] raises a KeyError because "apple" is a value, not a key.
- A dictionary can use a string such as "fav" as a key, and assigning a value to an existing key replaces its previous value.
- Keys are unique, but values may repeat; a for-loop over a dictionary iterates through its keys, which can then retrieve associated values.
- Professor Rosen identifies counting, categorizing items into lists, and looking up account details by a unique identifier such as an email address as common uses.
- The character-frequency algorithm initializes counts as an empty dictionary and loops through every character in every line.
- The in keyword checks whether a character is already a key: unseen characters get an initial count of 1, while seen characters are incremented.
- The resulting dictionary counts all encountered characters without requiring a predefined 128-element range or a separate branch for each character.
- For each line, split() separates whitespace-delimited words, and a nested loop processes each word.
- The same dictionary pattern assigns new words a count of 1 and increments counts for words already present.
- A filtering loop prints only words whose counts reach at least 200, making the frequency results easier to inspect.
- Calling lower() before counting combines capitalization variants such as "The" and "the" into one dictionary key.
- The processed Treasure Island text contains roughly 4,541 instances of "the" after case normalization.
- lower() does not remove punctuation, so tokens can retain periods or apostrophes; consistent text cleaning depends on handling such cases.
- Calling sorted(counts) returns the dictionary’s keys in their default order, which is alphabetical for words and follows character ordering for punctuation.
- To order keys according to their associated counts, pass counts.get as the key function to sorted(), as in sorted(counts, key=counts.get).
- The key parameter determines each item’s sort value; reversing the result can be used when descending frequency order is needed.
- A function such as f(x), which returns 2 * x, can be assigned another name; assigning g = f makes g an alias for the same function.
- Functions can be passed as arguments: a function that accepts another function and x can apply the passed function and then transform its result.
- This first-class treatment lets programs store and pass functions much like other values, rather than requiring every operation to be hard-coded.
- A lambda expression creates a small function inline, for example lambda x: 3 * x, without defining a named function first.
- map(function, iterable) applies a function to each item; wrapping map(f, L) in list() materializes the results in modern Python.
- Combining map with a lambda can transform a list in one expression, such as summing values after tripling each item.
- The final project may use Python, C, C++, Java, Swift, Kotlin, Haskell, Go, or another programming language.
- Students may use Unity, Unreal, or Godot; game-engine projects can produce polished results and count for 10% of the grade.
- Professor Rosen encourages experimenting with programming tools and beginning project planning before the final-project period.
Transcript excerpt
0:01 All right. So, um administrative announcement so a couple administrative announcements first, and then we will talk about uh the content for the day. So, right? Uh as I sent out in the announcement, which may have been buried in the 104 emails that got sent to you, um the deadline for the assign for the for um all your work is So, not all your work. For lab four and five is Monday, right? The idea here is that if you have not done lab four and five by that point, you are probably irreparably behind in the class. It's not that I can't be lenient and uh
0:48 and and like let students make up work at that point. I have in the past. It's just my experience of having done that has shown that that's never really worked out for the student. It's incurred a lot more stress on them. It's a clear incurred a lot more work on me. And at that point and it and the outcome's always the same. It's still the student does extremely poorly on the exams because they're still that far behind and just struggling to catch up. Um So, that's the issue. Uh that that's the reason why we have that set up. Um the So, just make sure that lab four and five is done. It also kind of removes some students early who have just not shown up. Believe it or not, when I was going through and doing uh W's and U's yesterday, there were a number of
1:34 students with less than 1% in the class. That means that they did not show up, or they have shown up and done precisely like one in-class exercise, or one runestone assignment, and then nothing else. Like at that point, what do we do? So,
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Professor Rosen.