Concepts / Filtering Dictionary Data

Filtering Dictionary Data

When a dictionary appears directly after in, the loop visits its keys, not its values.

  • Programming

The Loop Variable

A dictionary contains key-value pairs, but a for loop does not automatically give you both parts when the dictionary is used directly as the sequence. The loop variable receives one key at a time. This behavior is the starting point for working with, and eventually filtering, dictionary data.

Key point

When a dictionary appears directly after in, the loop visits its keys, not its values.

Tracing Key Visits

Imagine a dictionary whose keys were inserted in this order: chuck, annie, and jan. During a direct dictionary loop, the loop variable first holds chuck, then annie, and finally jan. The loop does not jump straight to the associated values. Each iteration begins with the current key.

dictionarykeys: chuck, annie, janchuckfirst keyanniesecond keyjanthird keyfirst iterationnext iterationnext iteration
What does the loop variable receive at each step when a dictionary is used directly in a for loop?

The diagram shows the loop's progression. After the iteration for jan, there are no more keys, so the loop ends. Keeping this sequence in mind makes it easier to trace a loop manually before deciding what data should be selected.

Finding Each Value

The loop variable gives you the current key. To obtain the corresponding value, use bracket notation with that key. If the current key is stored in key and the dictionary is stored in counts, the expression counts[key] retrieves the value associated with the current key.

keycurrent keychucka dictionary keyanniea dictionary keycounts[chuck]associated valuecounts[annie]associated valuecurrent iterationbracket lookuplater iterationbracket lookup
How does a key encountered in the loop connect to its corresponding value in the dictionary?
Definition

Direct dictionary iteration is the process of visiting each dictionary key in sequence. Accessing both parts of a pair requires two steps: receive the current key in the loop variable, then use bracket notation to retrieve that key's value.

Worked Trace

Worked example

Tracing counts

A dictionary has keys inserted in the order chuck, annie, and jan. A loop visits the dictionary directly and prints the current key together with the value retrieved from counts[key]. Predict the three iterations.

  1. 1
    First iteration

    The loop variable holds chuck. The lookup counts[chuck] retrieves the value associated with chuck, so the loop prints chuck and that value.

  2. 2
    Second iteration

    The loop variable holds annie. The lookup counts[annie] retrieves the value associated with annie, so the loop prints annie and that value.

  3. 3
    Third iteration

    The loop variable holds jan. The lookup counts[jan] retrieves the value associated with jan, so the loop prints jan and that value.

  4. 4
    End of loop

    There are no more keys after jan, so the loop ends.

Result

The keys are processed in this order: chuck, annie, jan. Each key is paired with the value retrieved by using that key in the dictionary.

What do you think happens?

What key is held by the loop variable on the second iteration when the keys were inserted as chuck, annie, and jan?

  • chuck

  • annie

  • jan

Reveal answer
Answer:

annie

Dictionary keys maintain insertion order, so the second visited key is annie.

The important output prediction is not based on alphabetical order or on the values. It is based on the order in which the keys were inserted. Once the current key is known, bracket notation identifies the value that accompanies it.

Mistakes During Filtering

  • Common mistake

    Expecting a direct dictionary loop to produce values

    What it looks like

    Treating the loop variable as though it already contains the value associated with the current key

    Why it fails

    Direct iteration over a dictionary visits keys, not values.

    Do this instead

    Use the current key inside bracket notation to retrieve its corresponding value.

  • Common mistake

    Ignoring insertion order when predicting output

    What it looks like

    Predicting that chuck, annie, and jan will be visited alphabetically

    Why it fails

    The keys are visited in their insertion order.

    Do this instead

    Write down the order in which the keys were added, then trace the loop using that order.

  • Common mistake

    Trying to reason about the value before identifying the key

    What it looks like

    Starting with an unknown output value instead of determining which key the loop currently holds

    Why it fails

    The value lookup depends on the current key.

    Do this instead

    Trace each iteration in two steps: identify the key, then retrieve the value associated with it.

Best practice

When debugging or predicting a dictionary loop, make a small trace with one row per iteration. Record the current key first, then record the value obtained by looking up that key. This mirrors the actual two-step behavior of the loop.

Practice and Review

EASY

A dictionary's keys were inserted in this order: red, blue, green. Describe the key held by the loop variable on each iteration of a direct dictionary loop. Then explain how the corresponding value would be retrieved for each key.

Hints
  • The loop visits keys rather than values.
  • Use insertion order to determine the sequence.
  • The current key is used inside bracket notation to retrieve its value.

This same trace is the foundation for filtering dictionary data. Before deciding which entries to keep, you must be able to visit each key, retrieve its associated value, and understand the order in which the entries are examined.

  1. 1
    A direct for loop over a dictionary visits one key at a time. The loop variable contains the current key, not its value. Use bracket notation with that key to retrieve the corresponding value. Dictionary keys maintain insertion order, so you can predict the loop's sequence by recording the order in which the keys were added.

Key Takeaways

  • A dictionary used directly in a for loop is traversed by key.
  • The loop variable contains one current key at a time.
  • Bracket notation retrieves the value associated with the current key.
  • Keys are visited in insertion order, making loop output predictable.
  • To trace or filter dictionary data, identify the key first and then inspect its associated value.

Related concepts

More concepts starting with F