liveBook · Manning

Chapter 7: Dictionaries

What you will learn in chapter 7 of The Quick Python Book, Fourth Edition

Introduces dictionaries as Python’s useful associative arrays or maps, implemented using hash tables.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces dictionaries as Python’s useful associative arrays or maps, implemented using hash tables.

Key terms: dictionaries, associative arrays, maps, hash tables

7.1 What is a dictionary?

This section introduces dictionaries as key-to-value mappings, contrasts them with lists, and shows how to create, populate, and use them.

Key terms: dictionary, key, mapping, list index, empty dictionary, curly braces, assignment, list indices, dictionary keys, order they were added

7.2 Other dictionary operations

This section explains how to inspect, look up, copy, and modify dictionaries beyond basic key assignment and access.

Key terms: keys, values, items, dictionary view objects, creation order, in, get, None, setdefault, defaultdict

7.3 Word counting

Shows how a dictionary can count occurrences of words in a file and notes why this pattern is useful.

Key terms: dictionary key, get() method, count, dictionary operations, Counter, collections module

7.4 What can be used as a key?

This section explains which objects can be dictionary keys and why tuples work as composite keys only when their contents are hashable.

Key terms: dictionary key, immutable, hashable, mutable, tuple, hash value, __hash__, mutable objects

7.5 Sparse matrices

This section explains how a dictionary can store a large matrix efficiently when most of its elements are zero.

Key terms: matrix, sparse matrices, nonzero elements, dictionaries, tuple indices, row and column, get method

7.6 Dictionaries as caches

This section explains how a dictionary can cache results of an expensive function so repeated calls with the same arguments do not recalculate them.

Key terms: cache, dictionary, tuples as keys, argument combinations

7.7 Efficiency of dictionaries

This section explains why efficiency concerns usually should not deter readers from choosing a dictionary when it makes a solution cleaner.

Key terms: dictionaries, lists (arrays), efficient, unacceptable slowdown

7.8 Word counting

This lab outlines how to count words in a previously prepared file with a dictionary and report the most and least common words.

Key terms: dictionary, keys, values, word counts, items(), tuples

7.8.1 Solving the problem with AI-generated code

This subsection shows how to prompt an AI chatbot to solve the word-counting exercise with a dictionary and specified output.

Key terms: prompt, dictionary, Counter class, moby_01_clean.txt

7.8.2 Solutions and discussion

7.9 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 6: Strings Chapter 8: Control flow →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.