Dictionaries are very useful for archivists, as they are ideal for holding data with description, and
key value pairs will be familiar to all archivists who work with metadata. The dict.py sample script
provides an example of a useful calendar dictionary on line 7. You can get values from a dictionary by
calling keys with brackets. Here we get the value of the “04” key from the calendar dictionary just like in
line 10.
6| #a useful dictionary
7| calendar = {"01": "January", "02": "February", "03": "March", "04":
"April", "05": "May", "06": "June", "07": "July", "08": "August", "09":
"September", "10": "October", "11": "November", "12": "December"}
8|
9| #print the value for the key "04"
10| print (calendar["04"])
> April
Next, we see that both list and dictionaries can be nested within one another. You can have a
list of dictionaries, or dictionaries that contain lists as values. On lines 12-17 we set up a set of
dictionaries that contain the keys: title, dates, and extent. The first two keys contain strings and the
extent key contains a list with both an integer and a string. Finally, we add all of these dictionaries to a
single list called fonds on line 20.
12| #both lists and dictionaries can be nested
13| collection = {"title": "Hugo A. Bedau Papers", "dates": "1957-2003 ",
"extent": [36, "cubic ft."]}
14| series1 = {"title": "Scholarship and Teaching", "dates": "1958-2002",
"extent": [6, "cubic ft."]}
15| series2 = {"title": "Correspondence", "dates": "1957-2003", "extent": [4,
"cubic ft."]}
16| series3 = {"title": "Advocacy Organization", "dates": "1958-2003",
"extent": [10, "cubic ft."]}
17| series4 = {"title": "Subject Files", "dates": "1955-2003", "extent":
[16.79, "cubic ft."]}
18|
19| #here is a list of the dictionaries above
20| fonds = [collection, series1, series2, series3, series4]
We can loop though this list with for and print values by calling the relevant keys. On lines 22-23 we
print the title and dates keys for each dictionary in the fonds list. On lines 26-27 we loop through the
extent lists and print each item after turning it into a string with str().