Application: sparse matrices
20-minute lesson · module-dictionaries-04
You are previewing this course as a guest
Read anything and run any exercise you like. It all runs in your own browser, so nothing here is saved: no XP, no progress, and the AI tutor needs an account. Sign in and your work starts counting.
Application: sparse matrices
20-minute lesson · module-dictionaries-04
- Explain why storing a mostly-zero matrix as a full list of lists wastes space.
- Represent a sparse matrix as a dictionary keyed by (row, col) tuples.
- Read a cell, treating missing keys as zero with `.get`.
- Convert between the dense (list-of-lists) and sparse (dict) representations.
Source attribution
Status: adapted
How to Think Like a Computer Scientist: Interactive Edition — source · Dictionaries / Sparse matrices
Adaptation notes. Reading rewritten in our voice. The worked example frames the matrix as a course-prerequisite adjacency grid for an Iowa State CS plan of study. Hidden tests check tuple-keyed access with `.get` defaults and round-trip conversion between dense and sparse forms.
License notes. Original chapter is GNU FDL 1.3 (Brad Miller, David Ranum, and the upstream Elkner/Downey/Meyers text). No verbatim text reproduced.