|
1 .. 8.4 LO: dictionaries (2) |
|
2 .. ------------------------ |
|
3 .. * empty |
|
4 .. * filled |
|
5 .. * accessing via keys |
|
6 .. * .values(), .keys() |
|
7 .. * in |
|
8 .. * iteration |
|
9 |
|
10 ============ |
|
11 Dictionaries |
|
12 ============ |
|
13 |
|
14 {{{ show the welcome slide }}} |
|
15 |
|
16 Welcome to the spoken tutorial on dictionaries. |
|
17 |
|
18 {{{ switch to next slide, outline slide }}} |
|
19 |
|
20 In this tutorial, we will see how to create empty dictionaries, learn |
|
21 about keys and values of dictionaries. Checking for elements and |
|
22 iterating over elements. |
|
23 |
|
24 {{{ switch to next slide on overview of dictionaries }}} |
|
25 |
|
26 A dictionary in general, is designed to look up meanings of |
|
27 words. Similarly, Python dictionary is also designed to look up for a |
|
28 specific key and retrieve the corresponding value. Dictionaries are |
|
29 data structures that provide key-value mappings. Dictionaries are |
|
30 similar to lists except that instead of the values having integer |
|
31 indexes, dictionaries have keys or strings as indexes. |
|
32 |
|
33 Before we can proceed, start your IPython interpreter with the |
|
34 ``-pylab`` option. |
|
35 |
|
36 {{{ start ipython interpreter by issuing command ipython -pylab }}} |
|
37 |
|
38 Let us start by creating an empty dictionary, type the following in |
|
39 your IPython interpreter. |
|
40 :: |
|
41 |
|
42 mt_dict = {} |
|
43 |
|
44 Notice that unlike lists curly braces are used define ``dictionary``, |
|
45 |
|
46 {{{ move the mouse over curly braces to grab attention }}} |
|
47 |
|
48 Now let us see how to create a filled dictionary, |
|
49 :: |
|
50 |
|
51 extensions = {'jpg' : 'JPEG Image', 'py' : 'Python script', 'html' : 'Html document', 'pdf' : 'Portable Document Format'} |
|
52 |
|
53 Notice that each key-value pair is separated by a comma |
|
54 |
|
55 {{{ move the mouse over the commas to grab attention }}} |
|
56 |
|
57 and each key and value are separated using a colon. |
|
58 |
|
59 {{{ move the mouse over the colon one by one to grab attention }}} |
|
60 |
|
61 Here, we defined four entries in the dictionary extensions. The keys |
|
62 are |
|
63 |
|
64 {{{ spell the keys letter by letter }}} |
|
65 |
|
66 jpg, py, html, and pdf. |
|
67 |
|
68 Simply type, |
|
69 :: |
|
70 |
|
71 extensions |
|
72 |
|
73 in the interpreter to see the content of the dictionary. Notice that |
|
74 in dictionaries the order cannot be predicted and you can see that the |
|
75 values are not in the order that we entered in. |
|
76 |
|
77 Like in lists, the elements in a dictionary can be accessed using the |
|
78 index, here the index is the key. Try, |
|
79 :: |
|
80 |
|
81 print extensions['jpg'] |
|
82 |
|
83 It printed JPEG Image. And now try, |
|
84 :: |
|
85 |
|
86 print extensions['zip'] |
|
87 |
|
88 Well it gave us an error, saying that the key 'zip' is not in the |
|
89 dictionary. |
|
90 |
|
91 Pause here for some time and try few more keys. Also try jpg in |
|
92 capital letters. |
|
93 |
|
94 {{{ switch to next slide, adding and deleting keys and values in |
|
95 dictionaries }}} |
|
96 |
|
97 Well that was about creating dictionaries, now how do we add or delete |
|
98 items. We can add new items into dictionaries as, |
|
99 :: |
|
100 |
|
101 extensions['cpp'] = 'C++ code' |
|
102 |
|
103 and delete items using the ``del`` keyword as, |
|
104 :: |
|
105 |
|
106 del extension['pdf'] |
|
107 |
|
108 Let us check the content of the dictionary now, |
|
109 :: |
|
110 |
|
111 extensions |
|
112 |
|
113 So the changes have been made. Now let us try one more thing, |
|
114 :: |
|
115 |
|
116 extensions['cpp'] = 'C++ source code' |
|
117 extensions |
|
118 |
|
119 As you can see, it did not add a new thing nor gave an error, but it |
|
120 simply replaces the existing value with the new one. |
|
121 |
|
122 Now let us learn how to check if a particular key is present in the |
|
123 dictionary. For that we can use ``in``, |
|
124 :: |
|
125 |
|
126 'py' in extensions |
|
127 'odt' in extensions |
|
128 |
|
129 So in short it will return ``True`` if the key is found in the |
|
130 dictionary, and will return ``False`` if key is not present. Note that |
|
131 we can check only for container-ship of keys in dictionaries and not |
|
132 values. |
|
133 |
|
134 {{{ switch to next slide, Retrieve keys and values }}} |
|
135 |
|
136 Now let us see how to retrieve the keys and values. We can use the |
|
137 method ``keys()`` for getting a list of the keys in a particular |
|
138 dictionary and the method ``values()`` for getting a list of |
|
139 values. Let us try them, |
|
140 :: |
|
141 |
|
142 extensions.keys() |
|
143 |
|
144 It returned the ``list`` of keys in the dictionary extensions. And now |
|
145 the other one, |
|
146 :: |
|
147 |
|
148 extensions.values() |
|
149 |
|
150 It returned the ``list`` of values in the dictionary. |
|
151 |
|
152 {{{ switch to next slide, problem statement for the next solved |
|
153 exercise }}} |
|
154 |
|
155 Now let us try to print the data in the dictionary. We can use ``for`` |
|
156 loop to iterate. |
|
157 :: |
|
158 |
|
159 for each in extensions.keys(): |
|
160 print each, "-->", extensions[each] |
|
161 |
|
162 |
|
163 {{{ switch to next slide, recap }}} |
|
164 |
|
165 This brings us to the end of this tutorial, we learned dictionaries |
|
166 and saw how to create an empty dictionary, build a dictionary with |
|
167 some data in it, adding data, ``keys()`` and ``values()`` methods, and |
|
168 iterating over the dictionaries. |
|
169 |
|
170 {{{ switch to next slide, thank you slide }}} |
|
171 |
|
172 Thank you! |
|
173 |
|
174 .. Author: Anoop Jacob Thomas <anoop@fossee.in> |
|
175 Reviewer 1: |
|
176 Reviewer 2: |
|
177 External reviewer: |