Concepts / Dictionary Basics: Creating and Accessing

Dictionary Basics: Creating and Accessing

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.

  • Programming
Interactive lab

Try it: Counting Words with a Dictionary

The py4e histogram pattern: split each line into words, count them with counts[word] = counts.get(word, 0) + 1, then find the most common word with a max loop or by sorting (count, word) tuples in reverse.

How it works

  1. Optionally remove punctuation with str.translate and lower-case the line, then split() it into words.
  2. For each word: counts.get(word, 0) returns the current count or 0 for a new word; store it plus one (or the if word not in counts form).
  3. A dict remembers insertion order: a word's position is fixed the first time it is seen.
  4. Max loop: keep the word whose count is strictly larger than the best so far (the first word wins a tie).
  5. Or build [(count, word) …], sort with reverse=True (ties ordered by the word, reversed) and slice [:N].

Default run (25 steps): Start with an empty dict. The text has 3 lines. … [:3] keeps the top 3: [(5, 'the'), (2, 'sat'), (2, 'on')].

Simplified: Printable-ASCII text up to 320 characters, 8 lines and 60 words, typed in the lab instead of read from a file or URL.

Educational simulation

Loading the simulation…

From Mail Log to Useful Summary

Imagine a mail log containing thousands of messages. The raw messages are difficult to compare directly, but a dictionary can transform them into a frequency summary. In a histogram, each unique value becomes a dictionary key and the value stored for that key is the number of times it appears.

Key point

The same counting pattern can answer several related questions. First count messages by full email address, then find the sender with the largest count, and finally count by domain to analyze organizations rather than individual senders.

Building the Email Histogram

A histogram uses a dictionary as a frequency counter. Each email address is a key. When an address appears for the first time, its count starts at zero and is immediately increased to one. When the address appears again, the existing count is retrieved and increased by one. The pattern counts[key] = counts.get(key, 0) + 1 handles both cases in one line.

ana@example.orgfirst appearanceana@example.orgcount: 2ana@example.orgsecond appearancebo@sample.netcount: 1bo@sample.netone appearancecreates countincrements countcreates count
How does each email address become a unique dictionary key, and how does its count change when the address appears again?
Code walkthrough
emails = [
    "ana@example.org",
    "bo@sample.net",
    "ana@example.org",
    "ana@example.org",
    "bo@sample.net"
]

counts = {}
for email in emails:
    counts[email] = counts.get(email, 0) + 1

print(counts)
  1. 1
    The first appearance of ana@example.org creates a count of 1.
  2. 2
    The first appearance of bo@sample.net creates a count of 1.
  3. 3
    The next two appearances of ana@example.org raise its count to 3.
  4. 4
    The second appearance of bo@sample.net raises its count to 2.
  • The empty dictionary will hold the histogram.
  • Each email is used as a dictionary key.
  • get(email, 0) supplies zero when the key is not present.
  • Adding one records the current appearance.
Output
{'ana@example.org': 3, 'bo@sample.net': 2}
ana@example.orgdictionary keycountsemail histogram3stored frequencylookupreturns value
Given an email address, how does the program locate its corresponding message count?

Finding the Largest Sender Count

After building the histogram, the most prolific sender can be found with a maximum loop. The loop examines each dictionary entry and keeps two pieces of information: the key belonging to the best entry so far and that entry's count. When a larger count is found, both tracked values are replaced.

startbest sender: none; bestcount: 0ana@example.orgcount: 3bo@sample.netcount: 2ana@example.orgwinning sender; count: 3examine entryexamine next entryretain larger count
As the loop examines each dictionary entry, how do the current maximum count and winning sender change?
python
Output (expected)
ana@example.org
3

The two tracking variables must be updated together. The count identifies whether the current entry is better, while the sender records which key produced that count. At the end of the loop, the tracked sender and tracked count describe the highest-frequency dictionary entry.

From Sender Names to Domains

A full email address answers an individual-sender question. A domain answers an organization-level question. To extract the domain, split the email address at the @ symbol and use the second part: email.split('@')[1]. The find method can also locate the @ symbol, after which string slicing can take the text from the position after that symbol.

ana@example.orgoriginal emailsplit('@')separate at @anafirst partexample.orgsecond partapplypart 1part 2
How does splitting an email address at the @ symbol produce a username and a domain?
Code walkthrough
domain_counts = {}

for email in emails:
    domain = email.split('@')[1]
    domain_counts[domain] = domain_counts.get(domain, 0) + 1

print(domain_counts)
  1. 1
    Each full email address is converted into its domain.
  2. 2
    Addresses with the same domain use the same dictionary key.
  3. 3
    The domain count increases each time any address from that domain appears.
  • The domain_counts dictionary stores frequencies by domain.
  • split('@')[1] selects the text after the @ symbol.
  • The same histogram pattern counts each extracted domain.
Output
{'example.org': 3, 'sample.net': 2}
ana@example.orgcount: 2example.orgcount: 3lee@example.orgcount: 1sample.netcount: 1bo@sample.netcount: 1same domainsame domainsame domain
How do multiple full email addresses become one domain key, and how does that change the frequency counts?

Mistakes in Histogram Analysis

  • Common mistake

    Using a new count of one every time instead of increasing the existing count.

    What it looks like
    counts[email] = 1
    Why it fails

    A repeated key is overwritten with one, so the dictionary does not retain the frequency of earlier appearances.

    Do this instead
    Use counts[email] = counts.get(email, 0) + 1.
  • Common mistake

    Tracking only the largest count during the maximum loop.

    What it looks like

    largest_count is updated, but no sender key is stored.

    Why it fails

    The final number identifies the frequency but not which dictionary key produced it.

    Do this instead

    Track both the sender key and its count whenever a larger count is found.

  • Common mistake

    Counting full email addresses when the question is about organizations.

    What it looks like

    Using email as the key when comparing domains.

    Why it fails

    Different people at the same domain remain separate keys.

    Do this instead

    Extract domain = email.split('@')[1] and use domain as the histogram key.

  • Common mistake

    Selecting the first part after splitting an email address.

    What it looks like
    domain = email.split('@')[0]
    Why it fails

    The first part is the text before the @ symbol, not the domain.

    Do this instead

    Use the second part, email.split('@')[1], for the domain.

Best practice

Before writing the loop, decide what one dictionary key should represent. If the key is a full email address, the result measures individual senders. If the key is a domain, the result measures combined activity from organizations. The histogram pattern stays the same; only the value used as the key changes.

Practice the Three Transformations

MEDIUM

Given the email sequence ana@example.org, bo@sample.net, lee@example.org, ana@example.org, and bo@sample.net, write a program that creates a full-address histogram, finds the sender with the highest count, and creates a domain histogram.

Hints
  • Start with an empty dictionary for full email counts.
  • Use counts[email] = counts.get(email, 0) + 1.
  • For the maximum loop, keep both the winning key and its count.
  • Extract each domain with email.split('@')[1], then apply the same histogram pattern.

What do you think happens?

Before running the domain histogram, predict the count for example.org when ana@example.org appears twice and lee@example.org appears once.

  • 1

  • 2

  • 3

  • The domain is not counted

Reveal answer
Answer:

3

Changing the key from the full email address to the domain combines both senders under example.org, so their appearances contribute to one domain count.

Key Takeaways

  1. 1
    A dictionary histogram maps each unique key to the number of times that key appears.
  2. 2
    The pattern counts[key] = counts.get(key, 0) + 1 handles both new and existing keys.
  3. 3
    A maximum loop must track both the highest count and the key associated with it.
  4. 4
    email.split('@')[1] extracts the domain for organization-level counting.
  5. 5
    The choice of key controls the granularity and meaning of the analysis.

At a glance

  • Use dictionaries as frequency counters for repeated values.
  • Count full email addresses to compare individual senders.
  • Track both key and value in a maximum loop to identify the most frequent sender.
  • Extract domains with email.split('@')[1] when the analysis should group senders by organization.
  • Changing the dictionary key changes the question answered by the histogram.

More concepts starting with D