Concepts / Using Dictionaries to Count and Aggregate Data

Using Dictionaries to Count and Aggregate Data

Keys and values are the two components of a key-value pair (item). Keys are unique identifiers; values are the data they point to.

  • Programming

One Item, One Association

Suppose you need to keep track of how many times each item appears. The central challenge is not only storing numbers; it is connecting each number to the item it describes. A dictionary solves this by storing associations. Each association connects a key, which identifies what you want to find, with a value, which is the data connected to that key.

A key-value pair, also called an item, consists of a key and the value associated with that key. The key is the unique identifier, and the value is the data the key points to.

identifiesconnects toitemunique identifierkey-value pairitemcountassociated data
Which part identifies the data, which part stores the data, and how are they connected?

Reading a Dictionary

A lookup is the operation of providing a key and receiving the corresponding value. For example, in an expression such as my_dict['name'], the key is name and the operation is a lookup. The dictionary uses that key to retrieve the value associated with it.

The key is not the data you are trying to retrieve; it is the identifier used to locate that data. The value is the result connected to the key. In a counting task, the value can be a tally associated with a unique item. This is why a dictionary is useful for a histogram: each unique item can be associated with its count.

A Small Counting Dictionary

Count the occurrences of the items red, blue, red, and red.

Process red: The item red becomes a key. Its associated value is a count of one.

Process blue: The item blue becomes another key. Its associated value is a count of one.

Process red again: Red is already a key, so its associated count is increased rather than creating a second red key.

Process red once more: The existing red association is updated again, producing a count of three for red.

The resulting associations are red with a count of three and blue with a count of one.

From Key to Location

A dictionary lookup can be described at two levels. At the visible level, you provide a key and receive its value. At the implementation level, Python dictionaries use a hashtable. A hashtable uses a hash function to convert a key into a numerical position. That position helps the hashtable locate where the key and its associated value should be stored or retrieved.

inputmaps tolocatesnamelookup keyhash functionconverts keynumerical positionlocationassociated valuelookup result
How does a dictionary transform a key into a location and use that location to find the associated value?

Updating Counts as Data Arrives

Counting and aggregation follow the same association pattern. The item being processed acts as a key, and its count or aggregate result acts as the value. When an item appears for the first time, the dictionary needs an association for that unique item. When the same item appears again, the relevant value is the tally being continued. The important distinction is that the key identifies the item, while the value records the current result for that item.

process redprocess blueprocess redprocess redempty dictionaryno associationsred: 1first occurrencered: 1, blue: 1two unique itemsred: 2, blue: 1red count updatedred: 3, blue: 1final tallies
What changes in the dictionary when each item is processed and its key's count is updated?

This pattern is often called building a histogram. In this context, a histogram is a collection of tallies. The dictionary connects every unique item to its tally, allowing the collection of counts to be represented as key-value pairs.

What do you think happens?

After processing red, blue, red, and red, how many keys should the counting dictionary contain?

  • One key
  • Two keys
  • Four keys
Reveal answer

Answer: Two keys

The keys identify unique items. Red appears three times but remains one key; blue is the second key. Their values hold the counts.

Terminology Traps

  • Calling the value the identifier

    The key is the unique identifier used to look up data. The value is the data associated with that key.

    Fix: Describe the item being counted as the key and its tally as the value.

  • Treating repeated occurrences as repeated keys

    A dictionary associates a unique identifier with its data. Repeated occurrences can contribute to the value associated with the existing key.

    Fix: Keep red as one key and describe its changing tally as the value.

  • Using lookup and implementation as synonyms

    A lookup is the operation that takes a key and returns its value. A hashtable is the implementation used to support dictionary behavior.

    Fix: Say that a lookup uses the dictionary and that the hashtable is the underlying implementation.

  • Using key-value pair and value as if they mean the same thing

    An item, or key-value pair, includes both the key and its associated value.

    Fix: Use value for the associated data and key-value pair or item for the complete association.

Your Turn

EASY

Imagine processing the items apple, pear, apple, orange, pear, apple. Describe the final key-value pairs in a counting dictionary. Then identify which part of each pair is the key, which part is the value, and which operation would retrieve a value when given its key.

Hints
  • List each unique item only once as a key.
  • Count how many times each item appears.
  • A lookup takes a key and returns its corresponding value.

For a more advanced extension, consider why nested loops matter when processing or comparing dictionary contents. If an outer loop runs five times and an inner loop runs three times for each outer iteration, the inner loop body executes fifteen times in total.

Key Takeaways

  1. A key is a unique identifier, and a value is the data associated with it.
  2. A key-value pair, also called an item, contains both the key and the value.
  3. A lookup takes a key and returns its corresponding value.
  4. A hashtable is the implementation Python uses for dictionaries, and a hash function maps keys to numerical positions to support fast lookup.
  5. Dictionaries can represent histograms by associating each unique item with a count or tally.

Key Takeaways

  • Keys identify data, values store the associated data, and together they form key-value pairs.
  • A lookup uses a key to retrieve its corresponding value.
  • Python dictionaries are implemented with hashtables, which use hash functions to map keys to numerical positions.
  • Counting dictionaries represent histograms by associating each unique item with a tally.
  • Precise terminology helps distinguish dictionary operations from their underlying implementation.