3.1 The big three
In section
2.3, I informally introduced lists. These are objects of type list
and can contain items of any type. We access them via an int
index. They are mutable and in a defined order. In Figure 3.1, the list
on the left contains six items, two of which are duplicates.
A dictionary is an object of type dict
. A dictionary
has a set of unique keys that are associated with data values. The collection is mutable, but
the keys are not, and so are often numbers or strings. In the middle of Figure 3.1, the four
unique names are the keys pointing to data values.
Suppose you belong to a local library and have a library card number to check out books.
That number is unique to you, but can be used by the library staff to find your name and phone
number. We can use a dict
to implement this.
A set...