Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / tut / node7.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="tut.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="tut.html" title='Python Tutorial' />
8<link rel='contents' href='node2.html' title="Contents" />
9<link rel='index' href='node19.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="node8.html" />
13<link rel="prev" href="node6.html" />
14<link rel="parent" href="tut.html" />
15<link rel="next" href="node8.html" />
16<meta name='aesop' content='information' />
17<title>5. Data Structures </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="4. More Control Flow"
25 href="node6.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="Python Tutorial"
28 href="tut.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="6. Modules"
31 href="node8.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 Tutorial</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="node2.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><img src='../icons/blank.png'
38 border='0' height='32' alt='' width='32' /></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="node19.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="node6.html">4. More Control Flow</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="tut.html">Python Tutorial</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="node8.html">6. Modules</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54<div class='online-navigation'>
55<!--Table of Child-Links-->
56<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
57
58<UL CLASS="ChildLinks">
59<LI><A href="node7.html#SECTION007100000000000000000">5.1 More on Lists</a>
60<UL>
61<LI><A href="node7.html#SECTION007110000000000000000">5.1.1 Using Lists as Stacks</a>
62<LI><A href="node7.html#SECTION007120000000000000000">5.1.2 Using Lists as Queues</a>
63<LI><A href="node7.html#SECTION007130000000000000000">5.1.3 Functional Programming Tools</a>
64<LI><A href="node7.html#SECTION007140000000000000000">5.1.4 List Comprehensions</a>
65</ul>
66<LI><A href="node7.html#SECTION007200000000000000000">5.2 The <tt class="keyword">del</tt> statement</a>
67<LI><A href="node7.html#SECTION007300000000000000000">5.3 Tuples and Sequences</a>
68<LI><A href="node7.html#SECTION007400000000000000000">5.4 Sets</a>
69<LI><A href="node7.html#SECTION007500000000000000000">5.5 Dictionaries</a>
70<LI><A href="node7.html#SECTION007600000000000000000">5.6 Looping Techniques</a>
71<LI><A href="node7.html#SECTION007700000000000000000">5.7 More on Conditions</a>
72<LI><A href="node7.html#SECTION007800000000000000000">5.8 Comparing Sequences and Other Types</a>
73</ul>
74<!--End of Table of Child-Links-->
75</div>
76<HR>
77
78<H1><A NAME="SECTION007000000000000000000"></A><A NAME="structures"></A>
79<BR>
805. Data Structures
81</H1>
82
83<P>
84This chapter describes some things you've learned about already in
85more detail, and adds some new things as well.
86
87<P>
88
89<H1><A NAME="SECTION007100000000000000000"></A><A NAME="moreLists"></A>
90<BR>
915.1 More on Lists
92</H1>
93
94<P>
95The list data type has some more methods. Here are all of the methods
96of list objects:
97
98<P>
99<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
100 <td><nobr><b><tt id='l2h-9' xml:id='l2h-9' class="method">append</tt></b>(</nobr></td>
101 <td><var>x</var>)</td></tr></table></dt>
102<dd>
103Add an item to the end of the list;
104equivalent to <code>a[len(a):] = [<var>x</var>]</code>.
105</dl>
106
107<P>
108<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
109 <td><nobr><b><tt id='l2h-10' xml:id='l2h-10' class="method">extend</tt></b>(</nobr></td>
110 <td><var>L</var>)</td></tr></table></dt>
111<dd>
112Extend the list by appending all the items in the given list;
113equivalent to <code>a[len(a):] = <var>L</var></code>.
114</dl>
115
116<P>
117<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
118 <td><nobr><b><tt id='l2h-11' xml:id='l2h-11' class="method">insert</tt></b>(</nobr></td>
119 <td><var>i, x</var>)</td></tr></table></dt>
120<dd>
121Insert an item at a given position. The first argument is the index
122of the element before which to insert, so <code>a.insert(0, <var>x</var>)</code>
123inserts at the front of the list, and <code>a.insert(len(a), <var>x</var>)</code>
124is equivalent to <code>a.append(<var>x</var>)</code>.
125</dl>
126
127<P>
128<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
129 <td><nobr><b><tt id='l2h-12' xml:id='l2h-12' class="method">remove</tt></b>(</nobr></td>
130 <td><var>x</var>)</td></tr></table></dt>
131<dd>
132Remove the first item from the list whose value is <var>x</var>.
133It is an error if there is no such item.
134</dl>
135
136<P>
137<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
138 <td><nobr><b><tt id='l2h-13' xml:id='l2h-13' class="method">pop</tt></b>(</nobr></td>
139 <td><var></var><big>[</big><var>i</var><big>]</big><var></var>)</td></tr></table></dt>
140<dd>
141Remove the item at the given position in the list, and return it. If
142no index is specified, <code>a.pop()</code> removes and returns the last item
143in the list. The item is also removed from the list. (The square brackets
144around the <var>i</var> in the method signature denote that the parameter
145is optional, not that you should type square brackets at that
146position. You will see this notation frequently in the
147<em class="citetitle"><a
148 href="../lib/lib.html"
149 title="Python Library Reference"
150 >Python Library Reference</a></em>.)
151</dl>
152
153<P>
154<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
155 <td><nobr><b><tt id='l2h-14' xml:id='l2h-14' class="method">index</tt></b>(</nobr></td>
156 <td><var>x</var>)</td></tr></table></dt>
157<dd>
158Return the index in the list of the first item whose value is <var>x</var>.
159It is an error if there is no such item.
160</dl>
161
162<P>
163<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
164 <td><nobr><b><tt id='l2h-15' xml:id='l2h-15' class="method">count</tt></b>(</nobr></td>
165 <td><var>x</var>)</td></tr></table></dt>
166<dd>
167Return the number of times <var>x</var> appears in the list.
168</dl>
169
170<P>
171<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
172 <td><nobr><b><tt id='l2h-16' xml:id='l2h-16' class="method">sort</tt></b>(</nobr></td>
173 <td><var></var>)</td></tr></table></dt>
174<dd>
175Sort the items of the list, in place.
176</dl>
177
178<P>
179<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
180 <td><nobr><b><tt id='l2h-17' xml:id='l2h-17' class="method">reverse</tt></b>(</nobr></td>
181 <td><var></var>)</td></tr></table></dt>
182<dd>
183Reverse the elements of the list, in place.
184</dl>
185
186<P>
187An example that uses most of the list methods:
188
189<P>
190<div class="verbatim"><pre>
191&gt;&gt;&gt; a = [66.25, 333, 333, 1, 1234.5]
192&gt;&gt;&gt; print a.count(333), a.count(66.25), a.count('x')
1932 1 0
194&gt;&gt;&gt; a.insert(2, -1)
195&gt;&gt;&gt; a.append(333)
196&gt;&gt;&gt; a
197[66.25, 333, -1, 333, 1, 1234.5, 333]
198&gt;&gt;&gt; a.index(333)
1991
200&gt;&gt;&gt; a.remove(333)
201&gt;&gt;&gt; a
202[66.25, -1, 333, 1, 1234.5, 333]
203&gt;&gt;&gt; a.reverse()
204&gt;&gt;&gt; a
205[333, 1234.5, 1, 333, -1, 66.25]
206&gt;&gt;&gt; a.sort()
207&gt;&gt;&gt; a
208[-1, 1, 66.25, 333, 333, 1234.5]
209</pre></div>
210
211<P>
212
213<H2><A NAME="SECTION007110000000000000000"></A><A NAME="lists-as-stacks"></A>
214<BR>
2155.1.1 Using Lists as Stacks
216</H2>
217
218<P>
219The list methods make it very easy to use a list as a stack, where the
220last element added is the first element retrieved (``last-in,
221first-out''). To add an item to the top of the stack, use
222<tt class="method">append()</tt>. To retrieve an item from the top of the stack, use
223<tt class="method">pop()</tt> without an explicit index. For example:
224
225<P>
226<div class="verbatim"><pre>
227&gt;&gt;&gt; stack = [3, 4, 5]
228&gt;&gt;&gt; stack.append(6)
229&gt;&gt;&gt; stack.append(7)
230&gt;&gt;&gt; stack
231[3, 4, 5, 6, 7]
232&gt;&gt;&gt; stack.pop()
2337
234&gt;&gt;&gt; stack
235[3, 4, 5, 6]
236&gt;&gt;&gt; stack.pop()
2376
238&gt;&gt;&gt; stack.pop()
2395
240&gt;&gt;&gt; stack
241[3, 4]
242</pre></div>
243
244<P>
245
246<H2><A NAME="SECTION007120000000000000000"></A><A NAME="lists-as-queues"></A>
247<BR>
2485.1.2 Using Lists as Queues
249</H2>
250
251<P>
252You can also use a list conveniently as a queue, where the first
253element added is the first element retrieved (``first-in,
254first-out''). To add an item to the back of the queue, use
255<tt class="method">append()</tt>. To retrieve an item from the front of the queue,
256use <tt class="method">pop()</tt> with <code>0</code> as the index. For example:
257
258<P>
259<div class="verbatim"><pre>
260&gt;&gt;&gt; queue = ["Eric", "John", "Michael"]
261&gt;&gt;&gt; queue.append("Terry") # Terry arrives
262&gt;&gt;&gt; queue.append("Graham") # Graham arrives
263&gt;&gt;&gt; queue.pop(0)
264'Eric'
265&gt;&gt;&gt; queue.pop(0)
266'John'
267&gt;&gt;&gt; queue
268['Michael', 'Terry', 'Graham']
269</pre></div>
270
271<P>
272
273<H2><A NAME="SECTION007130000000000000000"></A><A NAME="functional"></A>
274<BR>
2755.1.3 Functional Programming Tools
276</H2>
277
278<P>
279There are three built-in functions that are very useful when used with
280lists: <tt class="function">filter()</tt>, <tt class="function">map()</tt>, and <tt class="function">reduce()</tt>.
281
282<P>
283"<tt class="samp">filter(<var>function</var>, <var>sequence</var>)</tt>" returns a sequence
284consisting of those items from the
285sequence for which <code><var>function</var>(<var>item</var>)</code> is true.
286If <var>sequence</var> is a <tt class="class">string</tt> or <tt class="class">tuple</tt>, the result will
287be of the same type; otherwise, it is always a <tt class="class">list</tt>.
288For example, to compute some primes:
289
290<P>
291<div class="verbatim"><pre>
292&gt;&gt;&gt; def f(x): return x % 2 != 0 and x % 3 != 0
293...
294&gt;&gt;&gt; filter(f, range(2, 25))
295[5, 7, 11, 13, 17, 19, 23]
296</pre></div>
297
298<P>
299"<tt class="samp">map(<var>function</var>, <var>sequence</var>)</tt>" calls
300<code><var>function</var>(<var>item</var>)</code> for each of the sequence's items and
301returns a list of the return values. For example, to compute some
302cubes:
303
304<P>
305<div class="verbatim"><pre>
306&gt;&gt;&gt; def cube(x): return x*x*x
307...
308&gt;&gt;&gt; map(cube, range(1, 11))
309[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
310</pre></div>
311
312<P>
313More than one sequence may be passed; the function must then have as
314many arguments as there are sequences and is called with the
315corresponding item from each sequence (or <code>None</code> if some sequence
316is shorter than another). For example:
317
318<P>
319<div class="verbatim"><pre>
320&gt;&gt;&gt; seq = range(8)
321&gt;&gt;&gt; def add(x, y): return x+y
322...
323&gt;&gt;&gt; map(add, seq, seq)
324[0, 2, 4, 6, 8, 10, 12, 14]
325</pre></div>
326
327<P>
328"<tt class="samp">reduce(<var>function</var>, <var>sequence</var>)</tt>" returns a single value
329constructed by calling the binary function <var>function</var> on the first two
330items of the sequence, then on the result and the next item, and so
331on. For example, to compute the sum of the numbers 1 through 10:
332
333<P>
334<div class="verbatim"><pre>
335&gt;&gt;&gt; def add(x,y): return x+y
336...
337&gt;&gt;&gt; reduce(add, range(1, 11))
33855
339</pre></div>
340
341<P>
342If there's only one item in the sequence, its value is returned; if
343the sequence is empty, an exception is raised.
344
345<P>
346A third argument can be passed to indicate the starting value. In this
347case the starting value is returned for an empty sequence, and the
348function is first applied to the starting value and the first sequence
349item, then to the result and the next item, and so on. For example,
350
351<P>
352<div class="verbatim"><pre>
353&gt;&gt;&gt; def sum(seq):
354... def add(x,y): return x+y
355... return reduce(add, seq, 0)
356...
357&gt;&gt;&gt; sum(range(1, 11))
35855
359&gt;&gt;&gt; sum([])
3600
361</pre></div>
362
363<P>
364Don't use this example's definition of <tt class="function">sum()</tt>: since summing
365numbers is such a common need, a built-in function
366<code>sum(<var>sequence</var>)</code> is already provided, and works exactly like
367this.
368
369<span class="versionnote">New in version 2.3.</span>
370
371<P>
372
373<H2><A NAME="SECTION007140000000000000000">
3745.1.4 List Comprehensions</A>
375</H2>
376
377<P>
378List comprehensions provide a concise way to create lists without resorting
379to use of <tt class="function">map()</tt>, <tt class="function">filter()</tt> and/or <tt class="keyword">lambda</tt>.
380The resulting list definition tends often to be clearer than lists built
381using those constructs. Each list comprehension consists of an expression
382followed by a <tt class="keyword">for</tt> clause, then zero or more <tt class="keyword">for</tt> or
383<tt class="keyword">if</tt> clauses. The result will be a list resulting from evaluating
384the expression in the context of the <tt class="keyword">for</tt> and <tt class="keyword">if</tt> clauses
385which follow it. If the expression would evaluate to a tuple, it must be
386parenthesized.
387
388<P>
389<div class="verbatim"><pre>
390&gt;&gt;&gt; freshfruit = [' banana', ' loganberry ', 'passion fruit ']
391&gt;&gt;&gt; [weapon.strip() for weapon in freshfruit]
392['banana', 'loganberry', 'passion fruit']
393&gt;&gt;&gt; vec = [2, 4, 6]
394&gt;&gt;&gt; [3*x for x in vec]
395[6, 12, 18]
396&gt;&gt;&gt; [3*x for x in vec if x &gt; 3]
397[12, 18]
398&gt;&gt;&gt; [3*x for x in vec if x &lt; 2]
399[]
400&gt;&gt;&gt; [[x,x**2] for x in vec]
401[[2, 4], [4, 16], [6, 36]]
402&gt;&gt;&gt; [x, x**2 for x in vec] # error - parens required for tuples
403 File "&lt;stdin&gt;", line 1, in ?
404 [x, x**2 for x in vec]
405 ^
406SyntaxError: invalid syntax
407&gt;&gt;&gt; [(x, x**2) for x in vec]
408[(2, 4), (4, 16), (6, 36)]
409&gt;&gt;&gt; vec1 = [2, 4, 6]
410&gt;&gt;&gt; vec2 = [4, 3, -9]
411&gt;&gt;&gt; [x*y for x in vec1 for y in vec2]
412[8, 6, -18, 16, 12, -36, 24, 18, -54]
413&gt;&gt;&gt; [x+y for x in vec1 for y in vec2]
414[6, 5, -7, 8, 7, -5, 10, 9, -3]
415&gt;&gt;&gt; [vec1[i]*vec2[i] for i in range(len(vec1))]
416[8, 12, -54]
417</pre></div>
418
419<P>
420List comprehensions are much more flexible than <tt class="function">map()</tt> and can be
421applied to complex expressions and nested functions:
422
423<P>
424<div class="verbatim"><pre>
425&gt;&gt;&gt; [str(round(355/113.0, i)) for i in range(1,6)]
426['3.1', '3.14', '3.142', '3.1416', '3.14159']
427</pre></div>
428
429<P>
430
431<H1><A NAME="SECTION007200000000000000000"></A><A NAME="del"></A>
432<BR>
4335.2 The <tt class="keyword">del</tt> statement
434</H1>
435
436<P>
437There is a way to remove an item from a list given its index instead
438of its value: the <tt class="keyword">del</tt> statement. Unlike the <tt class="method">pop()</tt>)
439method which returns a value, the <tt class="keyword">del</tt> keyword is a statement
440and can also be used to
441remove slices from a list (which we did earlier by assignment of an
442empty list to the slice). For example:
443
444<P>
445<div class="verbatim"><pre>
446&gt;&gt;&gt; a = [-1, 1, 66.25, 333, 333, 1234.5]
447&gt;&gt;&gt; del a[0]
448&gt;&gt;&gt; a
449[1, 66.25, 333, 333, 1234.5]
450&gt;&gt;&gt; del a[2:4]
451&gt;&gt;&gt; a
452[1, 66.25, 1234.5]
453</pre></div>
454
455<P>
456<tt class="keyword">del</tt> can also be used to delete entire variables:
457
458<P>
459<div class="verbatim"><pre>
460&gt;&gt;&gt; del a
461</pre></div>
462
463<P>
464Referencing the name <code>a</code> hereafter is an error (at least until
465another value is assigned to it). We'll find other uses for
466<tt class="keyword">del</tt> later.
467
468<P>
469
470<H1><A NAME="SECTION007300000000000000000"></A><A NAME="tuples"></A>
471<BR>
4725.3 Tuples and Sequences
473</H1>
474
475<P>
476We saw that lists and strings have many common properties, such as
477indexing and slicing operations. They are two examples of
478<a class="ulink" href="../lib/typesseq.html"
479 ><em>sequence</em> data types</a>. Since
480Python is an evolving language, other sequence data types may be
481added. There is also another standard sequence data type: the
482<em>tuple</em>.
483
484<P>
485A tuple consists of a number of values separated by commas, for
486instance:
487
488<P>
489<div class="verbatim"><pre>
490&gt;&gt;&gt; t = 12345, 54321, 'hello!'
491&gt;&gt;&gt; t[0]
49212345
493&gt;&gt;&gt; t
494(12345, 54321, 'hello!')
495&gt;&gt;&gt; # Tuples may be nested:
496... u = t, (1, 2, 3, 4, 5)
497&gt;&gt;&gt; u
498((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
499</pre></div>
500
501<P>
502As you see, on output tuples are always enclosed in parentheses, so
503that nested tuples are interpreted correctly; they may be input with
504or without surrounding parentheses, although often parentheses are
505necessary anyway (if the tuple is part of a larger expression).
506
507<P>
508Tuples have many uses. For example: (x, y) coordinate pairs, employee
509records from a database, etc. Tuples, like strings, are immutable: it
510is not possible to assign to the individual items of a tuple (you can
511simulate much of the same effect with slicing and concatenation,
512though). It is also possible to create tuples which contain mutable
513objects, such as lists.
514
515<P>
516A special problem is the construction of tuples containing 0 or 1
517items: the syntax has some extra quirks to accommodate these. Empty
518tuples are constructed by an empty pair of parentheses; a tuple with
519one item is constructed by following a value with a comma
520(it is not sufficient to enclose a single value in parentheses).
521Ugly, but effective. For example:
522
523<P>
524<div class="verbatim"><pre>
525&gt;&gt;&gt; empty = ()
526&gt;&gt;&gt; singleton = 'hello', # &lt;-- note trailing comma
527&gt;&gt;&gt; len(empty)
5280
529&gt;&gt;&gt; len(singleton)
5301
531&gt;&gt;&gt; singleton
532('hello',)
533</pre></div>
534
535<P>
536The statement <code>t = 12345, 54321, 'hello!'</code> is an example of
537<em>tuple packing</em>: the values <code>12345</code>, <code>54321</code> and
538<code>'hello!'</code> are packed together in a tuple. The reverse operation
539is also possible:
540
541<P>
542<div class="verbatim"><pre>
543&gt;&gt;&gt; x, y, z = t
544</pre></div>
545
546<P>
547This is called, appropriately enough, <em>sequence unpacking</em>.
548Sequence unpacking requires the list of variables on the left to
549have the same number of elements as the length of the sequence. Note
550that multiple assignment is really just a combination of tuple packing
551and sequence unpacking!
552
553<P>
554There is a small bit of asymmetry here: packing multiple values
555always creates a tuple, and unpacking works for any sequence.
556
557<P>
558
559<H1><A NAME="SECTION007400000000000000000"></A><A NAME="sets"></A>
560<BR>
5615.4 Sets
562</H1>
563
564<P>
565Python also includes a data type for <em>sets</em>. A set is an unordered
566collection with no duplicate elements. Basic uses include membership
567testing and eliminating duplicate entries. Set objects also support
568mathematical operations like union, intersection, difference, and
569symmetric difference.
570
571<P>
572Here is a brief demonstration:
573
574<P>
575<div class="verbatim"><pre>
576&gt;&gt;&gt; basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
577&gt;&gt;&gt; fruit = set(basket) # create a set without duplicates
578&gt;&gt;&gt; fruit
579set(['orange', 'pear', 'apple', 'banana'])
580&gt;&gt;&gt; 'orange' in fruit # fast membership testing
581True
582&gt;&gt;&gt; 'crabgrass' in fruit
583False
584
585&gt;&gt;&gt; # Demonstrate set operations on unique letters from two words
586...
587&gt;&gt;&gt; a = set('abracadabra')
588&gt;&gt;&gt; b = set('alacazam')
589&gt;&gt;&gt; a # unique letters in a
590set(['a', 'r', 'b', 'c', 'd'])
591&gt;&gt;&gt; a - b # letters in a but not in b
592set(['r', 'd', 'b'])
593&gt;&gt;&gt; a | b # letters in either a or b
594set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
595&gt;&gt;&gt; a &amp; b # letters in both a and b
596set(['a', 'c'])
597&gt;&gt;&gt; a ^ b # letters in a or b but not both
598set(['r', 'd', 'b', 'm', 'z', 'l'])
599</pre></div>
600
601<P>
602
603<H1><A NAME="SECTION007500000000000000000"></A><A NAME="dictionaries"></A>
604<BR>
6055.5 Dictionaries
606</H1>
607
608<P>
609Another useful data type built into Python is the
610<a class="ulink" href="../lib/typesmapping.html"
611 ><em>dictionary</em></a>.
612Dictionaries are sometimes found in other languages as ``associative
613memories'' or ``associative arrays''. Unlike sequences, which are
614indexed by a range of numbers, dictionaries are indexed by <em>keys</em>,
615which can be any immutable type; strings and numbers can always be
616keys. Tuples can be used as keys if they contain only strings,
617numbers, or tuples; if a tuple contains any mutable object either
618directly or indirectly, it cannot be used as a key. You can't use
619lists as keys, since lists can be modified in place using methods like
620<tt class="method">append()</tt> and <tt class="method">extend()</tt> or modified with slice and
621indexed assignments.
622
623<P>
624It is best to think of a dictionary as an unordered set of
625<em>key: value</em> pairs, with the requirement that the keys are unique
626(within one dictionary).
627A pair of braces creates an empty dictionary: <code>{}</code>.
628Placing a comma-separated list of key:value pairs within the
629braces adds initial key:value pairs to the dictionary; this is also the
630way dictionaries are written on output.
631
632<P>
633The main operations on a dictionary are storing a value with some key
634and extracting the value given the key. It is also possible to delete
635a key:value pair
636with <code>del</code>.
637If you store using a key that is already in use, the old value
638associated with that key is forgotten. It is an error to extract a
639value using a non-existent key.
640
641<P>
642The <tt class="method">keys()</tt> method of a dictionary object returns a list of all
643the keys used in the dictionary, in arbitrary order (if you want it
644sorted, just apply the <tt class="method">sort()</tt> method to the list of keys). To
645check whether a single key is in the dictionary, either use the dictionary's
646<tt class="method">has_key()</tt> method or the <tt class="keyword">in</tt> keyword.
647
648<P>
649Here is a small example using a dictionary:
650
651<P>
652<div class="verbatim"><pre>
653&gt;&gt;&gt; tel = {'jack': 4098, 'sape': 4139}
654&gt;&gt;&gt; tel['guido'] = 4127
655&gt;&gt;&gt; tel
656{'sape': 4139, 'guido': 4127, 'jack': 4098}
657&gt;&gt;&gt; tel['jack']
6584098
659&gt;&gt;&gt; del tel['sape']
660&gt;&gt;&gt; tel['irv'] = 4127
661&gt;&gt;&gt; tel
662{'guido': 4127, 'irv': 4127, 'jack': 4098}
663&gt;&gt;&gt; tel.keys()
664['guido', 'irv', 'jack']
665&gt;&gt;&gt; tel.has_key('guido')
666True
667&gt;&gt;&gt; 'guido' in tel
668True
669</pre></div>
670
671<P>
672The <tt class="function">dict()</tt> constructor builds dictionaries directly from
673lists of key-value pairs stored as tuples. When the pairs form a
674pattern, list comprehensions can compactly specify the key-value list.
675
676<P>
677<div class="verbatim"><pre>
678&gt;&gt;&gt; dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
679{'sape': 4139, 'jack': 4098, 'guido': 4127}
680&gt;&gt;&gt; dict([(x, x**2) for x in (2, 4, 6)]) # use a list comprehension
681{2: 4, 4: 16, 6: 36}
682</pre></div>
683
684<P>
685Later in the tutorial, we will learn about Generator Expressions
686which are even better suited for the task of supplying key-values pairs to
687the <tt class="function">dict()</tt> constructor.
688
689<P>
690When the keys are simple strings, it is sometimes easier to specify
691pairs using keyword arguments:
692
693<P>
694<div class="verbatim"><pre>
695&gt;&gt;&gt; dict(sape=4139, guido=4127, jack=4098)
696{'sape': 4139, 'jack': 4098, 'guido': 4127}
697</pre></div>
698
699<P>
700
701<H1><A NAME="SECTION007600000000000000000"></A><A NAME="loopidioms"></A>
702<BR>
7035.6 Looping Techniques
704</H1>
705
706<P>
707When looping through dictionaries, the key and corresponding value can
708be retrieved at the same time using the <tt class="method">iteritems()</tt> method.
709
710<P>
711<div class="verbatim"><pre>
712&gt;&gt;&gt; knights = {'gallahad': 'the pure', 'robin': 'the brave'}
713&gt;&gt;&gt; for k, v in knights.iteritems():
714... print k, v
715...
716gallahad the pure
717robin the brave
718</pre></div>
719
720<P>
721When looping through a sequence, the position index and corresponding
722value can be retrieved at the same time using the
723<tt class="function">enumerate()</tt> function.
724
725<P>
726<div class="verbatim"><pre>
727&gt;&gt;&gt; for i, v in enumerate(['tic', 'tac', 'toe']):
728... print i, v
729...
7300 tic
7311 tac
7322 toe
733</pre></div>
734
735<P>
736To loop over two or more sequences at the same time, the entries
737can be paired with the <tt class="function">zip()</tt> function.
738
739<P>
740<div class="verbatim"><pre>
741&gt;&gt;&gt; questions = ['name', 'quest', 'favorite color']
742&gt;&gt;&gt; answers = ['lancelot', 'the holy grail', 'blue']
743&gt;&gt;&gt; for q, a in zip(questions, answers):
744... print 'What is your %s? It is %s.' % (q, a)
745...
746What is your name? It is lancelot.
747What is your quest? It is the holy grail.
748What is your favorite color? It is blue.
749</pre></div>
750
751<P>
752To loop over a sequence in reverse, first specify the sequence
753in a forward direction and then call the <tt class="function">reversed()</tt>
754function.
755
756<P>
757<div class="verbatim"><pre>
758&gt;&gt;&gt; for i in reversed(xrange(1,10,2)):
759... print i
760...
7619
7627
7635
7643
7651
766</pre></div>
767
768<P>
769To loop over a sequence in sorted order, use the <tt class="function">sorted()</tt>
770function which returns a new sorted list while leaving the source
771unaltered.
772
773<P>
774<div class="verbatim"><pre>
775&gt;&gt;&gt; basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
776&gt;&gt;&gt; for f in sorted(set(basket)):
777... print f
778...
779apple
780banana
781orange
782pear
783</pre></div>
784
785<P>
786
787<H1><A NAME="SECTION007700000000000000000"></A><A NAME="conditions"></A>
788<BR>
7895.7 More on Conditions
790</H1>
791
792<P>
793The conditions used in <code>while</code> and <code>if</code> statements can
794contain any operators, not just comparisons.
795
796<P>
797The comparison operators <code>in</code> and <code>not in</code> check whether a value
798occurs (does not occur) in a sequence. The operators <code>is</code> and
799<code>is not</code> compare whether two objects are really the same object; this
800only matters for mutable objects like lists. All comparison operators
801have the same priority, which is lower than that of all numerical
802operators.
803
804<P>
805Comparisons can be chained. For example, <code>a &lt; b == c</code> tests
806whether <code>a</code> is less than <code>b</code> and moreover <code>b</code> equals
807<code>c</code>.
808
809<P>
810Comparisons may be combined using the Boolean operators <code>and</code> and
811<code>or</code>, and the outcome of a comparison (or of any other Boolean
812expression) may be negated with <code>not</code>. These have lower
813priorities than comparison operators; between them, <code>not</code> has
814the highest priority and <code>or</code> the lowest, so that
815<code>A and not B or C</code> is equivalent to <code>(A and (not B)) or C</code>.
816As always, parentheses can be used to express the desired composition.
817
818<P>
819The Boolean operators <code>and</code> and <code>or</code> are so-called
820<em>short-circuit</em> operators: their arguments are evaluated from
821left to right, and evaluation stops as soon as the outcome is
822determined. For example, if <code>A</code> and <code>C</code> are true but
823<code>B</code> is false, <code>A and B and C</code> does not evaluate the
824expression <code>C</code>. When used as a general value and not as a
825Boolean, the return value of a short-circuit operator is the last
826evaluated argument.
827
828<P>
829It is possible to assign the result of a comparison or other Boolean
830expression to a variable. For example,
831
832<P>
833<div class="verbatim"><pre>
834&gt;&gt;&gt; string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
835&gt;&gt;&gt; non_null = string1 or string2 or string3
836&gt;&gt;&gt; non_null
837'Trondheim'
838</pre></div>
839
840<P>
841Note that in Python, unlike C, assignment cannot occur inside expressions.
842C programmers may grumble about this, but it avoids a common class of
843problems encountered in C programs: typing <code>=</code> in an expression when
844<code>==</code> was intended.
845
846<P>
847
848<H1><A NAME="SECTION007800000000000000000"></A><A NAME="comparing"></A>
849<BR>
8505.8 Comparing Sequences and Other Types
851</H1>
852
853<P>
854Sequence objects may be compared to other objects with the same
855sequence type. The comparison uses <em>lexicographical</em> ordering:
856first the first two items are compared, and if they differ this
857determines the outcome of the comparison; if they are equal, the next
858two items are compared, and so on, until either sequence is exhausted.
859If two items to be compared are themselves sequences of the same type,
860the lexicographical comparison is carried out recursively. If all
861items of two sequences compare equal, the sequences are considered
862equal. If one sequence is an initial sub-sequence of the other, the
863shorter sequence is the smaller (lesser) one. Lexicographical
864ordering for strings uses the ASCII ordering for individual
865characters. Some examples of comparisons between sequences of the
866same type:
867
868<P>
869<div class="verbatim"><pre>
870(1, 2, 3) &lt; (1, 2, 4)
871[1, 2, 3] &lt; [1, 2, 4]
872'ABC' &lt; 'C' &lt; 'Pascal' &lt; 'Python'
873(1, 2, 3, 4) &lt; (1, 2, 4)
874(1, 2) &lt; (1, 2, -1)
875(1, 2, 3) == (1.0, 2.0, 3.0)
876(1, 2, ('aa', 'ab')) &lt; (1, 2, ('abc', 'a'), 4)
877</pre></div>
878
879<P>
880Note that comparing objects of different types is legal. The outcome
881is deterministic but arbitrary: the types are ordered by their name.
882Thus, a list is always smaller than a string, a string is always
883smaller than a tuple, etc. <A NAME="tex2html3"
884 HREF="#foot736"><SUP>5.1</SUP></A> Mixed numeric types are compared according to their numeric value, so
8850 equals 0.0, etc.
886
887<P>
888<BR><HR><H4>Footnotes</H4>
889<DL>
890<DT><A NAME="foot736">... etc.</A><A
891 HREF="node7.html#tex2html3"><SUP>5.1</SUP></A></DT>
892<DD>
893 The rules for comparing objects of different types should
894 not be relied upon; they may change in a future version of
895 the language.
896
897
898</DD>
899</DL>
900<DIV CLASS="navigation">
901<div class='online-navigation'>
902<p></p><hr />
903<table align="center" width="100%" cellpadding="0" cellspacing="2">
904<tr>
905<td class='online-navigation'><a rel="prev" title="4. More Control Flow"
906 href="node6.html"><img src='../icons/previous.png'
907 border='0' height='32' alt='Previous Page' width='32' /></A></td>
908<td class='online-navigation'><a rel="parent" title="Python Tutorial"
909 href="tut.html"><img src='../icons/up.png'
910 border='0' height='32' alt='Up One Level' width='32' /></A></td>
911<td class='online-navigation'><a rel="next" title="6. Modules"
912 href="node8.html"><img src='../icons/next.png'
913 border='0' height='32' alt='Next Page' width='32' /></A></td>
914<td align="center" width="100%">Python Tutorial</td>
915<td class='online-navigation'><a rel="contents" title="Table of Contents"
916 href="node2.html"><img src='../icons/contents.png'
917 border='0' height='32' alt='Contents' width='32' /></A></td>
918<td class='online-navigation'><img src='../icons/blank.png'
919 border='0' height='32' alt='' width='32' /></td>
920<td class='online-navigation'><a rel="index" title="Index"
921 href="node19.html"><img src='../icons/index.png'
922 border='0' height='32' alt='Index' width='32' /></A></td>
923</tr></table>
924<div class='online-navigation'>
925<b class="navlabel">Previous:</b>
926<a class="sectref" rel="prev" href="node6.html">4. More Control Flow</A>
927<b class="navlabel">Up:</b>
928<a class="sectref" rel="parent" href="tut.html">Python Tutorial</A>
929<b class="navlabel">Next:</b>
930<a class="sectref" rel="next" href="node8.html">6. Modules</A>
931</div>
932</div>
933<hr />
934<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
935</DIV>
936<!--End of Navigation Panel-->
937<ADDRESS>
938See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
939</ADDRESS>
940</BODY>
941</HTML>