Dictionary Methods: get, keys, values, items
A histogram is a frequency count stored in a dictionary, where keys are unique values (like email addresses) and values are counts of how many times each key appears.
From Raw Messages to Questions
A mail log may contain thousands of messages, but raw lines do not immediately answer useful questions. A dictionary histogram transforms those messages into a frequency count. In this pattern, each unique value becomes a dictionary key, and the associated value records how many times that key appears. With email data, the key can be a full sender address or a domain.
Updating the Histogram
The central update pattern is counts[key] = counts.get(key, 0) + 1. The get method retrieves the current count for key. If key is not yet present, the second argument supplies 0. Adding 1 then records the next occurrence. The same line therefore handles both a new sender and a sender that has already appeared.
Three Messages from Two Senders
Count these generated sender values: ana@example.org, lee@example.net, and ana@example.org.
- 1First sender
The key ana@example.org is new, so get returns the default 0. Adding 1 stores a count of 1.
- 2Second sender
The key lee@example.net is new, so its count also becomes 1.
- 3Third sender
The key ana@example.org already has a count of 1. get retrieves 1, and adding 1 changes that value to 2.
The histogram is conceptually {ana@example.org: 2, lee@example.net: 1}. The keys are unique senders, and the values are their message counts.
Inspecting Dictionary Contents
Once the histogram exists, dictionary methods let you choose which part of the data to examine. keys produces the dictionary's unique keys, values produces the associated counts, and items provides each key together with its associated value. For a histogram, these choices support different questions: which senders exist, what counts were recorded, or which sender-count pairs should be examined together.
| Method | What it exposes | Useful histogram question |
|---|---|---|
| keys() | Unique email addresses or domains | Which senders or organizations appear? |
| values() | Counts associated with the keys | What frequencies were recorded? |
| items() | Each key together with its count | Which sender-count pair should be examined? |
The method choice determines which part of the histogram a loop can inspect.
Finding the Largest Count
To find the sender with the most messages, a maximum loop must track two pieces of information: the highest count seen so far and the key associated with that count. Iterating through items is useful because each iteration supplies both the sender and its count. When the current count is greater than the stored maximum, update both tracked values.
max_count = 0
max_sender = None
for sender, count in counts.items():
if count > max_count:
max_count = count
max_sender = sender- 1The first two assignments create the state that the loop will maintain.
- 2The loop examines each key-value pair in the histogram.
- 3The condition tests whether the current value is larger than the saved maximum.
- 4Both variables are updated when a new maximum is found.
- Start with no winning sender and a maximum count of 0.
items()supplies a sender and its count together.- Only a larger count replaces the current maximum.
- Update the count and the sender together so they remain associated.
Changing the Analysis Granularity
A full email address identifies an individual sender, while the domain portion shifts the grouping toward an organization. To extract the domain, split the email string on the @ character and take the second part: email.split('@')[1]. The source also describes an alternative using find to locate @ and slicing from the position after it. The split approach is often cleaner.
counts[email] = counts.get(email, 0) + 1The key represents the full email address, so counts are grouped by individual sender.
domain = email.split('@')[1]
counts[domain] = counts.get(domain, 0) + 1The key represents the extracted domain, so senders sharing a domain contribute to one organization-level count.
Changing the key changes the question being answered. Full-address keys preserve individual-sender detail. Domain keys combine messages from senders with the same domain. Neither grouping is universally better: each produces a different structured summary of the same raw messages.
Mistakes to Avoid
Common mistake Reading a missing key directly instead of providing a default.
What it looks likeUsing the current count for a sender before that sender has appeared in the dictionary.
Why it failsA new sender has no existing frequency to retrieve.
Do this insteadUse counts.get(key, 0) so a new key starts at zero before the occurrence is added.
Common mistake Finding the largest count without retaining its key.
What it looks likeTracking only max_count in the maximum loop.
Why it failsThe frequency is known, but the sender that produced it is lost.
Do this insteadTrack max_sender and max_count, and update both when a larger count is found.
Common mistake Counting full addresses when the question concerns organizations.
What it looks likeUsing email as the histogram key when the goal is to find the most frequent domain.
Why it failsDifferent senders from one domain remain separated into different keys.
Do this insteadExtract the domain with email.split('@')[1] and use that domain as the key.
Common mistake Confusing dictionary keys, values, and items.
What it looks likeUsing values when the loop needs both the sender and its count.
Why it failsValues provide counts, but they do not provide the associated keys.
Do this insteadUse items when the key and value must be processed together.
Choose the histogram key from the analytical question. Use the full email address when you need sender-level frequency. Extract the domain when you need organization-level frequency. Make that choice before updating the dictionary, because the key determines which records are grouped together.
Practice the Pattern
A generated message list contains these sender addresses: alex@north.example, maya@north.example, alex@north.example, and jo@west.example. Describe the full-address histogram, identify the sender with the highest count, then describe the domain histogram.
Hints
- Use the full email address as the key for the first histogram.
- For the maximum, compare each key-value pair while tracking both the winning key and its count.
- For the domain histogram, extract the part after @ before updating the dictionary.
Practice Check
Analyze the generated senders alex@north.example, maya@north.example, alex@north.example, and jo@west.example.
- 1Full-address grouping
The individual sender alex@north.example appears twice. maya@north.example and jo@west.example each appear once.
- 2Maximum search
The largest sender count is 2, associated with alex@north.example.
- 3Domain grouping
The two north.example senders combine into one domain key with a total of 3 messages. west.example has a total of 1.
The full-address analysis identifies the most prolific individual sender, while the domain analysis identifies the more frequent organization-level group.
Key Takeaways
- 1A dictionary histogram stores unique data values as keys and their frequencies as values.
- 2The pattern counts[key] =
counts.get(key, 0)+ 1 handles both new and existing keys. - 3Use items when a maximum loop must keep each key connected to its count.
- 4Use
email.split('@')[1] to change the grouping key from a full email address to a domain. - 5Changing the key changes the granularity of the analysis: full addresses describe senders, while domains combine senders by organization.
At a glance
- Dictionary histograms turn repeated raw values into frequency counts.
- get provides a safe starting value for a key that may not yet exist.
- keys, values, and items let you inspect different parts of a histogram.
- A maximum loop must track both the highest count and the key attached to it.
- The choice between full email addresses and domains determines whether the analysis is sender-level or organization-level.