| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="bltin-file-objects.html" /> |
| 13 | <link rel="prev" href="types-set.html" /> |
| 14 | <link rel="parent" href="types.html" /> |
| 15 | <link rel="next" href="bltin-file-objects.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>2.3.8 Mapping Types -- classdict </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="2.3.7 Set Types " |
| 25 | href="types-set.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="2.3 Built-in Types" |
| 28 | href="types.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="2.3.9 File Objects" |
| 31 | href="bltin-file-objects.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="types-set.html">2.3.7 Set Types </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="bltin-file-objects.html">2.3.9 File Objects</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION004380000000000000000"></A><A NAME="typesmapping"></A> |
| 56 | <BR> |
| 57 | 2.3.8 Mapping Types -- classdict |
| 58 | </H2> |
| 59 | <a id='l2h-227' xml:id='l2h-227'></a><a id='l2h-228' xml:id='l2h-228'></a> |
| 60 | <P> |
| 61 | A <i class="dfn">mapping</i> object maps immutable values to |
| 62 | arbitrary objects. Mappings are mutable objects. There is currently |
| 63 | only one standard mapping type, the <i class="dfn">dictionary</i>. A dictionary's keys are |
| 64 | almost arbitrary values. Only values containing lists, dictionaries |
| 65 | or other mutable types (that are compared by value rather than by |
| 66 | object identity) may not be used as keys. |
| 67 | Numeric types used for keys obey the normal rules for numeric |
| 68 | comparison: if two numbers compare equal (such as <code>1</code> and |
| 69 | <code>1.0</code>) then they can be used interchangeably to index the same |
| 70 | dictionary entry. |
| 71 | |
| 72 | <P> |
| 73 | Dictionaries are created by placing a comma-separated list of |
| 74 | <code><var>key</var>: <var>value</var></code> pairs within braces, for example: |
| 75 | <code>{'jack': 4098, 'sjoerd': 4127}</code> or |
| 76 | <code>{4098: 'jack', 4127: 'sjoerd'}</code>. |
| 77 | |
| 78 | <P> |
| 79 | The following operations are defined on mappings (where <var>a</var> and |
| 80 | <var>b</var> are mappings, <var>k</var> is a key, and <var>v</var> and <var>x</var> are |
| 81 | arbitrary objects): |
| 82 | <a id='l2h-229' xml:id='l2h-229'></a><a id='l2h-230' xml:id='l2h-230'></a><a id='l2h-231' xml:id='l2h-231'></a><a id='l2h-232' xml:id='l2h-232'></a><a id='l2h-234' xml:id='l2h-234'></a> |
| 83 | <P> |
| 84 | <div class="center"><table class="realtable"> |
| 85 | <thead> |
| 86 | <tr> |
| 87 | <th class="center">Operation</th> |
| 88 | <th class="left" >Result</th> |
| 89 | <th class="center">Notes</th> |
| 90 | </tr> |
| 91 | </thead> |
| 92 | <tbody> |
| 93 | <tr><td class="center" valign="baseline"><code>len(<var>a</var>)</code></td> |
| 94 | <td class="left" >the number of items in <var>a</var></td> |
| 95 | <td class="center"></td></tr> |
| 96 | <tr><td class="center" valign="baseline"><code><var>a</var>[<var>k</var>]</code></td> |
| 97 | <td class="left" >the item of <var>a</var> with key <var>k</var></td> |
| 98 | <td class="center">(1)</td></tr> |
| 99 | <tr><td class="center" valign="baseline"><code><var>a</var>[<var>k</var>] = <var>v</var></code></td> |
| 100 | <td class="left" >set <code><var>a</var>[<var>k</var>]</code> to <var>v</var></td> |
| 101 | <td class="center"></td></tr> |
| 102 | <tr><td class="center" valign="baseline"><code>del <var>a</var>[<var>k</var>]</code></td> |
| 103 | <td class="left" >remove <code><var>a</var>[<var>k</var>]</code> from <var>a</var></td> |
| 104 | <td class="center">(1)</td></tr> |
| 105 | <tr><td class="center" valign="baseline"><code><var>a</var>.clear()</code></td> |
| 106 | <td class="left" >remove all items from <code>a</code></td> |
| 107 | <td class="center"></td></tr> |
| 108 | <tr><td class="center" valign="baseline"><code><var>a</var>.copy()</code></td> |
| 109 | <td class="left" >a (shallow) copy of <code>a</code></td> |
| 110 | <td class="center"></td></tr> |
| 111 | <tr><td class="center" valign="baseline"><code><var>a</var>.has_key(<var>k</var>)</code></td> |
| 112 | <td class="left" ><code>True</code> if <var>a</var> has a key <var>k</var>, else <code>False</code></td> |
| 113 | <td class="center"></td></tr> |
| 114 | <tr><td class="center" valign="baseline"><code><var>k</var> <code>in</code> <var>a</var></code></td> |
| 115 | <td class="left" >Equivalent to <var>a</var>.has_key(<var>k</var>)</td> |
| 116 | <td class="center">(2)</td></tr> |
| 117 | <tr><td class="center" valign="baseline"><code><var>k</var> not in <var>a</var></code></td> |
| 118 | <td class="left" >Equivalent to <code>not</code> <var>a</var>.has_key(<var>k</var>)</td> |
| 119 | <td class="center">(2)</td></tr> |
| 120 | <tr><td class="center" valign="baseline"><code><var>a</var>.items()</code></td> |
| 121 | <td class="left" >a copy of <var>a</var>'s list of (<var>key</var>, <var>value</var>) pairs</td> |
| 122 | <td class="center">(3)</td></tr> |
| 123 | <tr><td class="center" valign="baseline"><code><var>a</var>.keys()</code></td> |
| 124 | <td class="left" >a copy of <var>a</var>'s list of keys</td> |
| 125 | <td class="center">(3)</td></tr> |
| 126 | <tr><td class="center" valign="baseline"><code><var>a</var>.update(<big>[</big><var>b</var><big>]</big>)</code></td> |
| 127 | <td class="left" >updates (and overwrites) key/value pairs from <var>b</var></td> |
| 128 | <td class="center">(9)</td></tr> |
| 129 | <tr><td class="center" valign="baseline"><code><var>a</var>.fromkeys(<var>seq</var><big>[</big>, <var>value</var><big>]</big>)</code></td> |
| 130 | <td class="left" >Creates a new dictionary with keys from <var>seq</var> and values set to <var>value</var></td> |
| 131 | <td class="center">(7)</td></tr> |
| 132 | <tr><td class="center" valign="baseline"><code><var>a</var>.values()</code></td> |
| 133 | <td class="left" >a copy of <var>a</var>'s list of values</td> |
| 134 | <td class="center">(3)</td></tr> |
| 135 | <tr><td class="center" valign="baseline"><code><var>a</var>.get(<var>k</var><big>[</big>, <var>x</var><big>]</big>)</code></td> |
| 136 | <td class="left" ><code><var>a</var>[<var>k</var>]</code> if <code><var>k</var> in <var>a</var></code>, |
| 137 | else <var>x</var></td> |
| 138 | <td class="center">(4)</td></tr> |
| 139 | <tr><td class="center" valign="baseline"><code><var>a</var>.setdefault(<var>k</var><big>[</big>, <var>x</var><big>]</big>)</code></td> |
| 140 | <td class="left" ><code><var>a</var>[<var>k</var>]</code> if <code><var>k</var> in <var>a</var></code>, |
| 141 | else <var>x</var> (also setting it)</td> |
| 142 | <td class="center">(5)</td></tr> |
| 143 | <tr><td class="center" valign="baseline"><code><var>a</var>.pop(<var>k</var><big>[</big>, <var>x</var><big>]</big>)</code></td> |
| 144 | <td class="left" ><code><var>a</var>[<var>k</var>]</code> if <code><var>k</var> in <var>a</var></code>, |
| 145 | else <var>x</var> (and remove k)</td> |
| 146 | <td class="center">(8)</td></tr> |
| 147 | <tr><td class="center" valign="baseline"><code><var>a</var>.popitem()</code></td> |
| 148 | <td class="left" >remove and return an arbitrary (<var>key</var>, <var>value</var>) pair</td> |
| 149 | <td class="center">(6)</td></tr> |
| 150 | <tr><td class="center" valign="baseline"><code><var>a</var>.iteritems()</code></td> |
| 151 | <td class="left" >return an iterator over (<var>key</var>, <var>value</var>) pairs</td> |
| 152 | <td class="center">(2), (3)</td></tr> |
| 153 | <tr><td class="center" valign="baseline"><code><var>a</var>.iterkeys()</code></td> |
| 154 | <td class="left" >return an iterator over the mapping's keys</td> |
| 155 | <td class="center">(2), (3)</td></tr> |
| 156 | <tr><td class="center" valign="baseline"><code><var>a</var>.itervalues()</code></td> |
| 157 | <td class="left" >return an iterator over the mapping's values</td> |
| 158 | <td class="center">(2), (3)</td></tr></tbody> |
| 159 | </table></div> |
| 160 | |
| 161 | <P> |
| 162 | Notes: |
| 163 | <DL> |
| 164 | <DT><STRONG>(1)</STRONG></DT> |
| 165 | <DD>Raises a <tt class="exception">KeyError</tt> exception if <var>k</var> is not |
| 166 | in the map. |
| 167 | |
| 168 | <P> |
| 169 | </DD> |
| 170 | <DT><STRONG>(2)</STRONG></DT> |
| 171 | <DD> |
| 172 | <span class="versionnote">New in version 2.2.</span> |
| 173 | |
| 174 | <P> |
| 175 | </DD> |
| 176 | <DT><STRONG>(3)</STRONG></DT> |
| 177 | <DD>Keys and values are listed in an arbitrary order which is |
| 178 | non-random, varies across Python implementations, and depends on the |
| 179 | dictionary's history of insertions and deletions. |
| 180 | If <tt class="method">items()</tt>, <tt class="method">keys()</tt>, <tt class="method">values()</tt>, |
| 181 | <tt class="method">iteritems()</tt>, <tt class="method">iterkeys()</tt>, and <tt class="method">itervalues()</tt> |
| 182 | are called with no intervening modifications to the dictionary, the |
| 183 | lists will directly correspond. This allows the creation of |
| 184 | <code>(<var>value</var>, <var>key</var>)</code> pairs using <tt class="function">zip()</tt>: |
| 185 | "<tt class="samp">pairs = zip(<var>a</var>.values(), <var>a</var>.keys())</tt>". The same |
| 186 | relationship holds for the <tt class="method">iterkeys()</tt> and |
| 187 | <tt class="method">itervalues()</tt> methods: "<tt class="samp">pairs = zip(<var>a</var>.itervalues(), |
| 188 | <var>a</var>.iterkeys())</tt>" provides the same value for <code>pairs</code>. |
| 189 | Another way to create the same list is "<tt class="samp">pairs = [(v, k) for (k, |
| 190 | v) in <var>a</var>.iteritems()]</tt>". |
| 191 | |
| 192 | <P> |
| 193 | </DD> |
| 194 | <DT><STRONG>(4)</STRONG></DT> |
| 195 | <DD>Never raises an exception if <var>k</var> is not in the map, |
| 196 | instead it returns <var>x</var>. <var>x</var> is optional; when <var>x</var> is not |
| 197 | provided and <var>k</var> is not in the map, <code>None</code> is returned. |
| 198 | |
| 199 | <P> |
| 200 | </DD> |
| 201 | <DT><STRONG>(5)</STRONG></DT> |
| 202 | <DD><tt class="function">setdefault()</tt> is like <tt class="function">get()</tt>, except |
| 203 | that if <var>k</var> is missing, <var>x</var> is both returned and inserted into |
| 204 | the dictionary as the value of <var>k</var>. <var>x</var> defaults to <var>None</var>. |
| 205 | |
| 206 | <P> |
| 207 | </DD> |
| 208 | <DT><STRONG>(6)</STRONG></DT> |
| 209 | <DD><tt class="function">popitem()</tt> is useful to destructively iterate |
| 210 | over a dictionary, as often used in set algorithms. If the dictionary |
| 211 | is empty, calling <tt class="function">popitem()</tt> raises a <tt class="exception">KeyError</tt>. |
| 212 | |
| 213 | <P> |
| 214 | </DD> |
| 215 | <DT><STRONG>(7)</STRONG></DT> |
| 216 | <DD><tt class="function">fromkeys()</tt> is a class method that returns a |
| 217 | new dictionary. <var>value</var> defaults to <code>None</code>. |
| 218 | <span class="versionnote">New in version 2.3.</span> |
| 219 | |
| 220 | <P> |
| 221 | </DD> |
| 222 | <DT><STRONG>(8)</STRONG></DT> |
| 223 | <DD><tt class="function">pop()</tt> raises a <tt class="exception">KeyError</tt> when no default |
| 224 | value is given and the key is not found. |
| 225 | <span class="versionnote">New in version 2.3.</span> |
| 226 | |
| 227 | <P> |
| 228 | </DD> |
| 229 | <DT><STRONG>(9)</STRONG></DT> |
| 230 | <DD><tt class="function">update()</tt> accepts either another mapping object |
| 231 | or an iterable of key/value pairs (as a tuple or other iterable of |
| 232 | length two). If keyword arguments are specified, the mapping is |
| 233 | then is updated with those key/value pairs: |
| 234 | "<tt class="samp">d.update(red=1, blue=2)</tt>". |
| 235 | |
| 236 | <span class="versionnote">Changed in version 2.4: |
| 237 | Allowed the argument to be an iterable of key/value |
| 238 | pairs and allowed keyword arguments.</span> |
| 239 | |
| 240 | <P> |
| 241 | </DD> |
| 242 | </DL> |
| 243 | |
| 244 | <P> |
| 245 | |
| 246 | <DIV CLASS="navigation"> |
| 247 | <div class='online-navigation'> |
| 248 | <p></p><hr /> |
| 249 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 250 | <tr> |
| 251 | <td class='online-navigation'><a rel="prev" title="2.3.7 Set Types " |
| 252 | href="types-set.html"><img src='../icons/previous.png' |
| 253 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 254 | <td class='online-navigation'><a rel="parent" title="2.3 Built-in Types" |
| 255 | href="types.html"><img src='../icons/up.png' |
| 256 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 257 | <td class='online-navigation'><a rel="next" title="2.3.9 File Objects" |
| 258 | href="bltin-file-objects.html"><img src='../icons/next.png' |
| 259 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 260 | <td align="center" width="100%">Python Library Reference</td> |
| 261 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 262 | href="contents.html"><img src='../icons/contents.png' |
| 263 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 264 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 265 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 266 | <td class='online-navigation'><a rel="index" title="Index" |
| 267 | href="genindex.html"><img src='../icons/index.png' |
| 268 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 269 | </tr></table> |
| 270 | <div class='online-navigation'> |
| 271 | <b class="navlabel">Previous:</b> |
| 272 | <a class="sectref" rel="prev" href="types-set.html">2.3.7 Set Types </A> |
| 273 | <b class="navlabel">Up:</b> |
| 274 | <a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A> |
| 275 | <b class="navlabel">Next:</b> |
| 276 | <a class="sectref" rel="next" href="bltin-file-objects.html">2.3.9 File Objects</A> |
| 277 | </div> |
| 278 | </div> |
| 279 | <hr /> |
| 280 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 281 | </DIV> |
| 282 | <!--End of Navigation Panel--> |
| 283 | <ADDRESS> |
| 284 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 285 | </ADDRESS> |
| 286 | </BODY> |
| 287 | </HTML> |