Dictionaries and Mapping

7. Dictionaries and Mapping#

Dictionaries are mapping containers for fast lookup, record-like data, counting, grouping, and key-value relationships. Sets use the same hashing model to store unique values, test membership quickly, and combine groups with set algebra.

  • 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

  • Sets store unique hashable values and support union, intersection, and difference

Chapter Video

Python Tutorial for Beginners 5: Dictionaries - Working with Key-Value Pairs (9:59) by Corey Schafer

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

Learning Objectives

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 and sets depend on hashable values

  6. Use sets and frozensets for removing duplicates, fast membership tests, and set algebra

Chapter Flow

Glossary

#

Term

Explanation

1

Dictionary

A mutable mapping that stores key-value pairs.

2

Mapping

A collection that connects each key to a corresponding value.

3

Key

The object used to look up a value in a dictionary.

4

Value

The object stored under a key in a dictionary.

5

Key-value pair

One association between a key and its value.

6

Hashable

Describes an object whose hash value is stable, so it can be used as a dictionary key or a set element.

7

Lookup

Retrieving a value by using its key.

8

Membership testing

Checking whether a key is present in a dictionary.

9

Adding/updating

Changing a dictionary by adding a new key-value pair or overwriting an existing value.

10

Delete items

Removing key-value pairs from a dictionary when they are no longer needed.

11

Common dictionary operations

Repeated dictionary tasks such as reading, adding, updating, deleting, and iterating.

12

get

A dictionary method that retrieves a value and can return a default when the key is missing.

13

items

A dictionary method that provides key-value pairs for iteration.

14

Dictionary comprehension

A compact expression for building a dictionary from an iterable.

15

value_counts

A counting function that returns how many times each value appears.

16

Counter

A dict subclass from collections designed for counting hashable values.

17

defaultdict

A dict subclass from collections that creates default values for missing keys.

18

Inverting a dictionary

Building a new dictionary that swaps the role of keys and values.

19

Sorting by value

Ordering dictionary entries using their values rather than their keys.

20

Tuple key

A tuple used as a compound dictionary key.

21

Set

An unordered, mutable collection of unique hashable values.

22

Frozenset

An immutable version of a set; it is hashable, so it can be a dictionary key or a set element.

23

Set operation

An operation such as union, intersection, difference, or symmetric difference.

24

Nested dictionary

A dictionary stored as a value inside another dictionary, often used for records and JSON data.

25

Hash table

The data structure that dictionaries and sets use to store items so that lookup takes about the same time no matter how many items there are.

26

Hash value

An integer computed by hash() from an object and used to locate that object in a hash table.

27

Accumulator

A variable that collects or accumulates data during a computation, such as a list built up inside a loop.

28

Filtering

Looping through a collection and keeping only the items that satisfy a condition.

29

Set comprehension

A compact expression in curly braces that builds a set from an iterable, discarding duplicates automatically.

30

Big O notation

A way of describing how the time or memory an operation needs grows with the size of the data, such as O(1) or O(n).

Chapter Overview Slides