---
title: "Dictionaries: Values Found by Key"
description: "A collection where each value is stored under a key of the programmer's choosing instead of a position. Looking a value up by key stays fast however many pairs are stored."
canonical: https://lightmysky.com/learn/computing/dictionaries-values-found-by-key-mt_mofpj0n5AM
source: https://lightmysky.com/learn/computing/dictionaries-values-found-by-key-mt_mofpj0n5AM.md
retrieved: 2026-09-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# Dictionaries: Values Found by Key

A collection where each value is stored under a key of the programmer's choosing instead of a position. Looking a value up by key stays fast however many pairs are stored.

Subject: Computing · Area: Programming · Ages 16 to 17
Page: https://lightmysky.com/learn/computing/dictionaries-values-found-by-key-mt_mofpj0n5AM

## Ready when they can

- Build a dictionary, read a value by key, and add or replace a pair
- Loop over keys and values to produce a summary such as a word count
- Say when a dictionary suits the data better than a list, and when it does not

## Lesson: Find it by name, not by place

A dictionary stores pairs, each with a key and a value, like a word and its definition. You write pairs in curly braces, as in {"apple": "red", "banana": "yellow"}. To read a value, you ask by key: forecast["temp"] hands back 4. No two keys in one dictionary may match, since each key must point to exactly one value.

**Example.** To count words, you walk the sentence and tally each word under its own key. Start empty, then for each word either add it with a count of 1 or raise its count by 1. The .get() helper gives 0 for a word seen first, so counts[word] = counts.get(word, 0) + 1 never fails. One pass over a whole book still works the same way.

Point keys toward the question you will ask, because lookup runs key to value only. A phone book keyed by country finds a calling code, but finding the country from a code needs the reversed dict, as in {1: "US", 91: "IN"}. Before reading, test with in, since brackets on a missing key raise an error. Loop with keys(), values() or items() to sum a field across records.

**Tip.** Pick a dictionary when you find things by name and a list when place or order matters. A list answers what sits third, while a dictionary answers what "temp" is. If you need both orders, you may need two structures, since one mapping runs a single way.

**Recap.** Keys name the values, lookup runs key to value, and tallies grow one pair at a time.

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [Lists: Many Values Under One Name](https://lightmysky.com/learn/computing/lists-many-values-under-one-name-mt_ipB6kmyK85)

## Opens up

- [Dynamic Programming: Optimal Substructure and Overlapping Subproblems](https://lightmysky.com/learn/computing/dynamic-programming-optimal-substructure-and-overlapping-subproblems-mt__hWkCDnzNY)
- [Hash Tables and Near-Constant Lookup](https://lightmysky.com/learn/computing/hash-tables-and-near-constant-lookup-mt_7v94MJEdmJ)
- [Classes and Objects: Bundling Data with Behaviour](https://lightmysky.com/learn/computing/classes-and-objects-bundling-data-with-behaviour-mt_kRvg7LZ_kk)
