Lesson 06: Dictionaries (associative array)
This week we continue our exploration of Python data structures with a focus on a new type: the associative array, known as a dictionary in Python.
You will learn how to utilize a for
loop in tandem with a dictionary’s keys()
, values()
, or
items()
methods.
As you start working with dictionaries you’ll want to familiarize yourself with the KeyError
exception.
Readings
Below are a set of readings that you should consider reviewing. Note that the Real Python articles referenced below may require signing up for a free account.
1.0 Required
Eric Matthes, Python Crash Course, 3rd Edition (No Starch Press, 2023).
Read Chapter 6 Dictionaries.
2.0 Recommended
Chad Hansen, Understanding the Python Traceback (RealPython, 2019).
We’ve been dipping in and out of Chad’s useful article. For this week skim the article again and
then read closely the subsection covering the KeyError
.
Charles Severance, Python for Everybody. Exploring data in Python 3 (CreateSpace, 2016).
Review the following chapters:
- Chapter 9 “Dictionaries”.
John Sturtz, Dictionaries in Python (Real Python, n.d).
Detailed article that includes a solid summary of available dictionary methods.
Lisa Tagliaferri, Understanding Dictionaries in Python 3 (Digital Ocean, Sept 2016).
Lisa offers a compact primer that I highly recommend.
Leodanis Pozo Ramis, How to Iterate Through a Dictionary in Python (RealPython, May 2019).
Leodanis discusses how to iterate over a dictionary using the dictionary’s items()
, keys()
,
and values()
methods.
3.0 Reference
Bookmark the following pages:
Eric Matthes, Beginner's Python Cheat Sheet - Dictionaries.
w3schools, Python Dictionary Methods (w3schools.com, nd).