Association Lists

OCW Scholar

« Previous | Next »

The idea of a dictionary is very important and useful in programming. A dictionary allows you to associate values with keys. In an actual English dictionary, the words are the keys and the definitions are the values. So, given a key, you can look up the value. In a phone book, names are the keys and phone numbers are the values.

Python has a built-in dictionary data structure, which you have read about already. We can make our own, simpler and less efficient data structure as a list of lists. It operates like this:

d = emptyAlist()
>>> addEntry(d, 4, 5)
>>> addEntry(d, 5, None)
>>> addEntry(d, 'ben', 'boa')
>>> addEntry(d, 'kim', 'krait')
>>> d
[[4, 5], [5, None], ['ben', 'boa'], ['kim', 'krait']]
>>> addEntry(d, 'bella', 'bi-colored-python-rock-snake')
>>> d
[[4, 5], [5, None], ['ben', 'boa'], ['kim', 'krait'], ['bella', 'bi-colored-python-rock-snake']]
>>> lookup(d, 'ben')
['ben', 'boa']
>>> lookup(d, 'biz')

The problems in the table below are taken from the 6.01 Python Tutor, an interactive environment that is not available on OCW. Do not try to answer these questions in the PDF files; answers will not be checked, and cannot be submitted.

PROBLEM # QUESTIONS
7.1.1 Add an entry to an alist (PDF)
7.1.2 Look up an entry in an alist (PDF)

 

« Previous | Next »