Page 214 -
P. 214
keys and values
Use a dictionary to associate data
Lists are great, but they are not always the best data structure for every
situation. Let’s take another look at Sarah’s data:
Sarah’s full name Sarah’s date of birth Sarah’s timing data
Sarah Sweeney,2002-6-17,2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55,2:22,2-21,2.22
There’s a definite structure here: the athlete’s name, the date of birth, and
then the list of times.
Let’s continue to use a list for the timing data, because that still makes sense.
But let’s make the timing data part of another data structure, which associates
all the data for an athlete with a single variable.
We’ll use a Python dictionary, which associates data values with keys:
The “keys”
Name "Sarah Sweeney"
The associated data, also known
as the “values”
DOB "2002-6-17"
Times [2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55,2:22,2-21,2.22]
Dictionary A built-in data structure (included with
Python) that allows you to associate data with keys, as
opposed to numbers. This lets your in-memory data
closely match the structure of your actual data.
178 Chapter 6