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
Python Tutorial for Beginners 5: Dictionaries by Corey Schafer
Learning Goals
By the end of this chapter, you will be able to:
Create, access, update, and delete entries in a Python dictionary
Use dictionary methods such as
.get(),.keys(),.values(),.items(), and.update()Write dictionary comprehensions and choose clear key-value structures
Use dictionary patterns for counting, grouping, inversion, and sorted summaries
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 |
defaultdict |
A |
comprehension |
A concise one-expression syntax for building dictionaries from iterables |
memoization |
Caching previously computed results in a dictionary to avoid redundant computation |