Dictionaries and Mapping

7. Dictionaries and Mapping#

Dictionaries are mapping containers for fast lookup, record-like data, counting, grouping, and key-value relationships.

  • Dictionaries map keys to values

  • Keys must be hashable so Python can locate values efficiently

  • Dictionary methods support lookup, mutation, iteration, and transformation

  • Comprehensions build dictionaries from existing data

  • Lookup-heavy programs often depend on dictionary design

Video

Learning Goals

By the end of this chapter, you will be able to:

  1. Create, access, update, and delete entries in a Python dictionary

  2. Use dictionary methods such as .get(), .keys(), .values(), .items(), and .update()

  3. Write dictionary comprehensions and choose clear key-value structures

  4. Use dictionary patterns for counting, grouping, inversion, and sorted summaries

  5. Explain why dictionaries depend on hashable values

Chapter Flow

Glossary

Term

Definition

dictionary

A mutable mapping that stores key-value pairs with O(1) average lookup by key

key

The identifier used to access a value in a dictionary; must be hashable and unique

value

The data associated with a key; can be any Python object

hash table

The underlying data structure enabling O(1) average lookup in dictionaries

Counter

A dict subclass from collections that counts element frequencies

defaultdict

A dict subclass from collections that auto-creates a default value for missing keys

comprehension

A concise one-expression syntax for building dictionaries from iterables

memoization

Caching previously computed results in a dictionary to avoid redundant computation

Chapter Overview Slides