Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 4 ■ ■ ■ Dictionaries: When Indices Won’t Do You’ve seen that lists are useful when you want to group values into a structure and refer to each value by number. In this chapter, you learn about a data structure in which you can refer to each value by name. This type of structure is called a mapping, and the only built-in mapping type in Python is the dictionary. The values in a dictionary don’t have any particular order but are stored under a key, which may be either a number, a string, or even a tuple. But What Are They For? There are many situations where a dictionary is more appropriate than a list. The name “dictionary” should give you a clue: an ordinary book is made for reading from start to finish. If you like, you can quickly open it to any given page. This is a bit like a Python list. Dictionaries, however (both real ones and their Python equivalent) are constructed so that you can look up a specific word (key) easily, to find its definition (value). Some arbitrary uses of Python dictionaries are as follows: • Representing the state of a gaming board, with each key being a tuple of coordinates • Storing file modification times, with file names as keys • A digital telephone/address book Let’s say you have a list of people: >>> names = ['Alice', 'Beth', 'Cecil', 'Dee-Dee', 'Earl'] What if you wanted to create a little database where you could store the telephone numbers of these people—how would you do that? One way would be to make another list. Let’s say you’re only storing their four-digit extensions. Then you would get something like this: >>> numbers = ['2341', '9102', '3158', '0142', '5551'] 67
68 CHAPTER 4 ■ DICTIONARIES: WHEN INDICES WON’T DO ■Note You might wonder why I have used strings to represent the telephone numbers—why not integers? Consider what would happen to Dee-Dee’s number then: >>> 0142 98 Not exactly what we wanted, is it? As mentioned briefly in Chapter 1, octal numbers are written with an initial zero. It is impossible to write decimal numbers like that. >>> 0912 File "", line 1 0912 ^ SyntaxError: invalid syntax The lesson is this: Telephone numbers (and other numbers that may contain leading zeros) should be represented as strings of digits—not integers. Once you’ve created these lists, you can look up Cecil’s telephone number as follows: >>> numbers[names.index('Cecil')] 3158 It works, but it’s a bit impractical. What you really would want to do is something like the following: >>> phonebook['Cecil'] 3158 Guess what? If phonebook is a dictionary, you can do just that. Dictionary Syntax Dictionaries are written like this: phonebook = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'} Dictionaries consist of pairs (called items) of keys and their corresponding values. In the preceding example, the names are the keys and the telephone numbers are the values. Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary (without any items) is written with just two curly braces, like this: {}. ■Note Keys are unique within a dictionary (and any other kind of mapping), while values may not be.
- Page 48 and 49: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 50 and 51: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 52 and 53: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 54 and 55: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 56 and 57: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 58 and 59: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 60 and 61: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 62 and 63: CHAPTER 2 ■ ■ ■ Lists and Tup
- Page 64 and 65: CHAPTER 2 ■ LISTS AND TUPLES 33 I
- Page 66 and 67: CHAPTER 2 ■ LISTS AND TUPLES 35 A
- Page 68 and 69: CHAPTER 2 ■ LISTS AND TUPLES 37 >
- Page 70 and 71: CHAPTER 2 ■ LISTS AND TUPLES 39 M
- Page 72 and 73: CHAPTER 2 ■ LISTS AND TUPLES 41 T
- Page 74 and 75: CHAPTER 2 ■ LISTS AND TUPLES 43 L
- Page 76 and 77: CHAPTER 2 ■ LISTS AND TUPLES 45 W
- Page 78 and 79: CHAPTER 2 ■ LISTS AND TUPLES 47
- Page 80 and 81: CHAPTER 2 ■ LISTS AND TUPLES 49 a
- Page 82 and 83: CHAPTER 2 ■ LISTS AND TUPLES 51 S
- Page 84 and 85: CHAPTER 3 ■ ■ ■ Working with
- Page 86 and 87: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 88 and 89: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 90 and 91: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 92 and 93: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 94 and 95: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 96 and 97: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 100 and 101: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 102 and 103: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 104 and 105: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 106 and 107: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 108 and 109: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 110: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 113 and 114: 82 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 115 and 116: 84 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 117 and 118: 86 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 119 and 120: 88 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 121 and 122: 90 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 123 and 124: 92 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 125 and 126: 94 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 127 and 128: 96 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 129 and 130: 98 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 131 and 132: 100 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 133 and 134: 102 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 135 and 136: 104 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 137 and 138: 106 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 139 and 140: 108 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 141 and 142: 110 CHAPTER 6 ■ ABSTRACTION But w
- Page 143 and 144: 112 CHAPTER 6 ■ ABSTRACTION ■Ti
- Page 145 and 146: 114 CHAPTER 6 ■ ABSTRACTION Can I
- Page 147 and 148: 116 CHAPTER 6 ■ ABSTRACTION stora
CHAPTER 4<br />
■ ■ ■<br />
Dictionaries: When Indices<br />
Won’t Do<br />
You’ve seen that lists are useful when you want <strong>to</strong> group values in<strong>to</strong> a structure and refer <strong>to</strong><br />
each value by number. In this chapter, you learn about a data structure in which you can refer<br />
<strong>to</strong> each value by name. This type of structure is called a mapping, and the only built-in mapping<br />
type in <strong>Python</strong> is the dictionary. The values in a dictionary don’t have any particular order but<br />
are s<strong>to</strong>red under a key, which may be either a number, a string, or even a tuple.<br />
But What Are They For?<br />
There are many situations where a dictionary is more appropriate than a list. The name<br />
“dictionary” should give you a clue: an ordinary book is made for reading from start <strong>to</strong> finish.<br />
If you like, you can quickly open it <strong>to</strong> any given page. This is a bit like a <strong>Python</strong> list. Dictionaries,<br />
however (both real ones and their <strong>Python</strong> equivalent) are constructed so that you can look up<br />
a specific word (key) easily, <strong>to</strong> find its definition (value). Some arbitrary uses of <strong>Python</strong> dictionaries<br />
are as follows:<br />
• Representing the state of a gaming board, with each key being a tuple of coordinates<br />
• S<strong>to</strong>ring file modification times, with file names as keys<br />
• A digital telephone/address book<br />
Let’s say you have a list of people:<br />
>>> names = ['Alice', 'Beth', 'Cecil', 'Dee-Dee', 'Earl']<br />
What if you wanted <strong>to</strong> create a little database where you could s<strong>to</strong>re the telephone numbers of<br />
these people—how would you do that? One way would be <strong>to</strong> make another list. Let’s say you’re<br />
only s<strong>to</strong>ring their four-digit extensions. Then you would get something like this:<br />
>>> numbers = ['2341', '9102', '3158', '0142', '5551']<br />
67