Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
11.1. Introduction: Dictionaries¶
The compound data types we have studied in detail so far — strings and lists — are sequential collections. This means that the items in the collection are ordered from left to right and they use integers as indices to access the values they contain. This also means that looking for a particular value requires scanning the many items in the list until you find the desired value.
Data can sometimes be organized more usefully by associating a key with the value we are looking for. For example, if you are asked for the page number for the start of chapter 5 in a large textbook, you might flip around the book looking for the chapter 5 heading. If the chapter number appears in the header or footer of each page, you might be able to find the page number fairly quickly but it’s generally easier and faster to go to the index page and see that chapter 5 starts on page 78.
This sort of direct look up of a value in Python is done with an object called a Dictionary. Dictionaries are a different kind of collection. They are Python’s built-in mapping type. A map is an unordered, associative collection. The association, or mapping, is from a key, which can be of any immutable type (e.g., the chapter name and number in the analogy above), to a value (the starting page number), which can be any Python data object. You’ll learn how to use these collections in the following chapter.
11.1.1. Learning Goals¶
To introduce the idea of Key, Value pairs
To introduce the idea of an unordered sequence
To understand the use of parallel construction in lists
To understand the performance benefit and simplicity of a dictionary over parallel lists
To understand that dictionary iteration iterates over keys
To correctly use the following:
The index operator to add a key,value pair to a dictionary
The del operator to remove an entry
index operator - retrieval by key
search - contains in / not in
get - with a default value