blob: b562558d9488f221d16438a024c4642277bcd74a [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. _tut-structures:
2
3***************
4Data Structures
5***************
6
7This chapter describes some things you've learned about already in more detail,
8and adds some new things as well.
9
Georg Brandl116aa622007-08-15 14:28:22 +000010.. _tut-morelists:
11
12More on Lists
13=============
14
15The list data type has some more methods. Here are all of the methods of list
16objects:
17
18
19.. method:: list.append(x)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000020 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000021
22 Add an item to the end of the list; equivalent to ``a[len(a):] = [x]``.
23
24
25.. method:: list.extend(L)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000026 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000027
28 Extend the list by appending all the items in the given list; equivalent to
29 ``a[len(a):] = L``.
30
31
32.. method:: list.insert(i, x)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000033 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000034
35 Insert an item at a given position. The first argument is the index of the
36 element before which to insert, so ``a.insert(0, x)`` inserts at the front of
37 the list, and ``a.insert(len(a), x)`` is equivalent to ``a.append(x)``.
38
39
40.. method:: list.remove(x)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000041 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000042
43 Remove the first item from the list whose value is *x*. It is an error if there
44 is no such item.
45
46
47.. method:: list.pop([i])
Christian Heimes4fbc72b2008-03-22 00:47:35 +000048 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000049
50 Remove the item at the given position in the list, and return it. If no index
51 is specified, ``a.pop()`` removes and returns the last item in the list. (The
52 square brackets around the *i* in the method signature denote that the parameter
53 is optional, not that you should type square brackets at that position. You
54 will see this notation frequently in the Python Library Reference.)
55
56
57.. method:: list.index(x)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000058 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000059
60 Return the index in the list of the first item whose value is *x*. It is an
61 error if there is no such item.
62
63
64.. method:: list.count(x)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000065 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000066
67 Return the number of times *x* appears in the list.
68
69
70.. method:: list.sort()
Christian Heimes4fbc72b2008-03-22 00:47:35 +000071 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000072
73 Sort the items of the list, in place.
74
75
76.. method:: list.reverse()
Christian Heimes4fbc72b2008-03-22 00:47:35 +000077 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000078
79 Reverse the elements of the list, in place.
80
81An example that uses most of the list methods::
82
83 >>> a = [66.25, 333, 333, 1, 1234.5]
Guido van Rossum0616b792007-08-31 03:25:11 +000084 >>> print(a.count(333), a.count(66.25), a.count('x'))
Georg Brandl116aa622007-08-15 14:28:22 +000085 2 1 0
86 >>> a.insert(2, -1)
87 >>> a.append(333)
88 >>> a
89 [66.25, 333, -1, 333, 1, 1234.5, 333]
90 >>> a.index(333)
91 1
92 >>> a.remove(333)
93 >>> a
94 [66.25, -1, 333, 1, 1234.5, 333]
95 >>> a.reverse()
96 >>> a
97 [333, 1234.5, 1, 333, -1, 66.25]
98 >>> a.sort()
99 >>> a
100 [-1, 1, 66.25, 333, 333, 1234.5]
101
102
103.. _tut-lists-as-stacks:
104
105Using Lists as Stacks
106---------------------
107
108.. sectionauthor:: Ka-Ping Yee <ping@lfw.org>
109
110
111The list methods make it very easy to use a list as a stack, where the last
112element added is the first element retrieved ("last-in, first-out"). To add an
113item to the top of the stack, use :meth:`append`. To retrieve an item from the
114top of the stack, use :meth:`pop` without an explicit index. For example::
115
116 >>> stack = [3, 4, 5]
117 >>> stack.append(6)
118 >>> stack.append(7)
119 >>> stack
120 [3, 4, 5, 6, 7]
121 >>> stack.pop()
122 7
123 >>> stack
124 [3, 4, 5, 6]
125 >>> stack.pop()
126 6
127 >>> stack.pop()
128 5
129 >>> stack
130 [3, 4]
131
132
133.. _tut-lists-as-queues:
134
135Using Lists as Queues
136---------------------
137
138.. sectionauthor:: Ka-Ping Yee <ping@lfw.org>
139
Ezio Melotti8f8db142010-03-31 07:45:32 +0000140It is also possible to use a list as a queue, where the first element added is
141the first element retrieved ("first-in, first-out"); however, lists are not
142efficient for this purpose. While appends and pops from the end of list are
143fast, doing inserts or pops from the beginning of a list is slow (because all
144of the other elements have to be shifted by one).
Georg Brandl116aa622007-08-15 14:28:22 +0000145
Ezio Melotti8f8db142010-03-31 07:45:32 +0000146To implement a queue, use :class:`collections.deque` which was designed to
147have fast appends and pops from both ends. For example::
Georg Brandl116aa622007-08-15 14:28:22 +0000148
Ezio Melotti8f8db142010-03-31 07:45:32 +0000149 >>> from collections import deque
150 >>> queue = deque(["Eric", "John", "Michael"])
Georg Brandl116aa622007-08-15 14:28:22 +0000151 >>> queue.append("Terry") # Terry arrives
152 >>> queue.append("Graham") # Graham arrives
Ezio Melotti8f8db142010-03-31 07:45:32 +0000153 >>> queue.popleft() # The first to arrive now leaves
Georg Brandl116aa622007-08-15 14:28:22 +0000154 'Eric'
Ezio Melotti8f8db142010-03-31 07:45:32 +0000155 >>> queue.popleft() # The second to arrive now leaves
Georg Brandl116aa622007-08-15 14:28:22 +0000156 'John'
Ezio Melotti8f8db142010-03-31 07:45:32 +0000157 >>> queue # Remaining queue in order of arrival
158 deque(['Michael', 'Terry', 'Graham'])
Georg Brandl718ce2c2010-03-21 09:51:44 +0000159
Georg Brandl116aa622007-08-15 14:28:22 +0000160
Georg Brandlfc11f272009-06-16 19:22:10 +0000161.. _tut-listcomps:
162
Georg Brandl116aa622007-08-15 14:28:22 +0000163List Comprehensions
164-------------------
165
Guido van Rossum0616b792007-08-31 03:25:11 +0000166List comprehensions provide a concise way to create lists from sequences.
167Common applications are to make lists where each element is the result of
Georg Brandl48310cd2009-01-03 21:18:54 +0000168some operations applied to each member of the sequence, or to create a
Guido van Rossum0616b792007-08-31 03:25:11 +0000169subsequence of those elements that satisfy a certain condition.
170
Georg Brandl7ae90dd2009-06-08 18:59:09 +0000171A list comprehension consists of brackets containing an expression followed
172by a :keyword:`for` clause, then zero or more :keyword:`for` or :keyword:`if`
173clauses. The result will be a list resulting from evaluating the expression in
174the context of the :keyword:`for` and :keyword:`if` clauses which follow it. If
175the expression would evaluate to a tuple, it must be parenthesized.
Guido van Rossum0616b792007-08-31 03:25:11 +0000176
177Here we take a list of numbers and return a list of three times each number::
178
179 >>> vec = [2, 4, 6]
180 >>> [3*x for x in vec]
181 [6, 12, 18]
182
183Now we get a little fancier::
184
Georg Brandle4ac7502007-09-03 07:10:24 +0000185 >>> [[x, x**2] for x in vec]
Guido van Rossum0616b792007-08-31 03:25:11 +0000186 [[2, 4], [4, 16], [6, 36]]
187
188Here we apply a method call to each item in a sequence::
Georg Brandl116aa622007-08-15 14:28:22 +0000189
190 >>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
191 >>> [weapon.strip() for weapon in freshfruit]
192 ['banana', 'loganberry', 'passion fruit']
Guido van Rossum0616b792007-08-31 03:25:11 +0000193
Georg Brandle4ac7502007-09-03 07:10:24 +0000194Using the :keyword:`if` clause we can filter the stream::
Guido van Rossum0616b792007-08-31 03:25:11 +0000195
Georg Brandl116aa622007-08-15 14:28:22 +0000196 >>> [3*x for x in vec if x > 3]
197 [12, 18]
198 >>> [3*x for x in vec if x < 2]
199 []
Guido van Rossum0616b792007-08-31 03:25:11 +0000200
201Tuples can often be created without their parentheses, but not here::
202
Georg Brandla1c6a1c2009-01-03 21:26:05 +0000203 >>> [x, x**2 for x in vec] # error - parens required for tuples
Georg Brandl116aa622007-08-15 14:28:22 +0000204 File "<stdin>", line 1, in ?
205 [x, x**2 for x in vec]
206 ^
207 SyntaxError: invalid syntax
208 >>> [(x, x**2) for x in vec]
209 [(2, 4), (4, 16), (6, 36)]
Guido van Rossum0616b792007-08-31 03:25:11 +0000210
Georg Brandle4ac7502007-09-03 07:10:24 +0000211Here are some nested for loops and other fancy behavior::
Guido van Rossum0616b792007-08-31 03:25:11 +0000212
Georg Brandl116aa622007-08-15 14:28:22 +0000213 >>> vec1 = [2, 4, 6]
214 >>> vec2 = [4, 3, -9]
215 >>> [x*y for x in vec1 for y in vec2]
216 [8, 6, -18, 16, 12, -36, 24, 18, -54]
217 >>> [x+y for x in vec1 for y in vec2]
218 [6, 5, -7, 8, 7, -5, 10, 9, -3]
219 >>> [vec1[i]*vec2[i] for i in range(len(vec1))]
220 [8, 12, -54]
221
Guido van Rossum0616b792007-08-31 03:25:11 +0000222List comprehensions can be applied to complex expressions and nested functions::
Georg Brandl116aa622007-08-15 14:28:22 +0000223
Georg Brandlf6945182008-02-01 11:56:49 +0000224 >>> [str(round(355/113, i)) for i in range(1, 6)]
Georg Brandl116aa622007-08-15 14:28:22 +0000225 ['3.1', '3.14', '3.142', '3.1416', '3.14159']
226
227
Christian Heimes0449f632007-12-15 01:27:15 +0000228Nested List Comprehensions
229--------------------------
230
231If you've got the stomach for it, list comprehensions can be nested. They are a
232powerful tool but -- like all powerful tools -- they need to be used carefully,
233if at all.
234
Georg Brandl48310cd2009-01-03 21:18:54 +0000235Consider the following example of a 3x3 matrix held as a list containing three
Christian Heimes0449f632007-12-15 01:27:15 +0000236lists, one list per row::
237
238 >>> mat = [
239 ... [1, 2, 3],
240 ... [4, 5, 6],
241 ... [7, 8, 9],
242 ... ]
243
Georg Brandl48310cd2009-01-03 21:18:54 +0000244Now, if you wanted to swap rows and columns, you could use a list
Christian Heimes0449f632007-12-15 01:27:15 +0000245comprehension::
246
Neal Norwitz752abd02008-05-13 04:55:24 +0000247 >>> print([[row[i] for row in mat] for i in [0, 1, 2]])
Christian Heimes0449f632007-12-15 01:27:15 +0000248 [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
249
250Special care has to be taken for the *nested* list comprehension:
251
252 To avoid apprehension when nesting list comprehensions, read from right to
253 left.
254
255A more verbose version of this snippet shows the flow explicitly::
256
257 for i in [0, 1, 2]:
258 for row in mat:
Neal Norwitz752abd02008-05-13 04:55:24 +0000259 print(row[i], end="")
Benjamin Petersonbfc644b2008-07-05 23:39:56 +0000260 print()
Christian Heimes0449f632007-12-15 01:27:15 +0000261
Mark Dickinson934896d2009-02-21 20:59:32 +0000262In real world, you should prefer built-in functions to complex flow statements.
Christian Heimes0449f632007-12-15 01:27:15 +0000263The :func:`zip` function would do a great job for this use case::
264
Georg Brandl409c9d72008-08-08 06:44:14 +0000265 >>> list(zip(*mat))
Christian Heimes0449f632007-12-15 01:27:15 +0000266 [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
267
268See :ref:`tut-unpacking-arguments` for details on the asterisk in this line.
269
Georg Brandl116aa622007-08-15 14:28:22 +0000270.. _tut-del:
271
272The :keyword:`del` statement
273============================
274
275There is a way to remove an item from a list given its index instead of its
276value: the :keyword:`del` statement. This differs from the :meth:`pop` method
277which returns a value. The :keyword:`del` statement can also be used to remove
278slices from a list or clear the entire list (which we did earlier by assignment
279of an empty list to the slice). For example::
280
281 >>> a = [-1, 1, 66.25, 333, 333, 1234.5]
282 >>> del a[0]
283 >>> a
284 [1, 66.25, 333, 333, 1234.5]
285 >>> del a[2:4]
286 >>> a
287 [1, 66.25, 1234.5]
288 >>> del a[:]
289 >>> a
290 []
291
292:keyword:`del` can also be used to delete entire variables::
293
294 >>> del a
295
296Referencing the name ``a`` hereafter is an error (at least until another value
297is assigned to it). We'll find other uses for :keyword:`del` later.
298
299
Georg Brandl5d955ed2008-09-13 17:18:21 +0000300.. _tut-tuples:
Georg Brandl116aa622007-08-15 14:28:22 +0000301
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000302Tuples and Sequences
303====================
304
305We saw that lists and strings have many common properties, such as indexing and
306slicing operations. They are two examples of *sequence* data types (see
307:ref:`typesseq`). Since Python is an evolving language, other sequence data
308types may be added. There is also another standard sequence data type: the
309*tuple*.
310
311A tuple consists of a number of values separated by commas, for instance::
312
313 >>> t = 12345, 54321, 'hello!'
314 >>> t[0]
315 12345
316 >>> t
317 (12345, 54321, 'hello!')
318 >>> # Tuples may be nested:
319 ... u = t, (1, 2, 3, 4, 5)
320 >>> u
321 ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
322
323As you see, on output tuples are always enclosed in parentheses, so that nested
324tuples are interpreted correctly; they may be input with or without surrounding
325parentheses, although often parentheses are necessary anyway (if the tuple is
326part of a larger expression).
327
328Tuples have many uses. For example: (x, y) coordinate pairs, employee records
329from a database, etc. Tuples, like strings, are immutable: it is not possible
330to assign to the individual items of a tuple (you can simulate much of the same
331effect with slicing and concatenation, though). It is also possible to create
332tuples which contain mutable objects, such as lists.
333
334A special problem is the construction of tuples containing 0 or 1 items: the
335syntax has some extra quirks to accommodate these. Empty tuples are constructed
336by an empty pair of parentheses; a tuple with one item is constructed by
337following a value with a comma (it is not sufficient to enclose a single value
338in parentheses). Ugly, but effective. For example::
339
340 >>> empty = ()
341 >>> singleton = 'hello', # <-- note trailing comma
342 >>> len(empty)
343 0
344 >>> len(singleton)
345 1
346 >>> singleton
347 ('hello',)
348
349The statement ``t = 12345, 54321, 'hello!'`` is an example of *tuple packing*:
350the values ``12345``, ``54321`` and ``'hello!'`` are packed together in a tuple.
351The reverse operation is also possible::
352
353 >>> x, y, z = t
354
Benjamin Petersond23f8222009-04-05 19:13:16 +0000355This is called, appropriately enough, *sequence unpacking* and works for any
Georg Brandl7ae90dd2009-06-08 18:59:09 +0000356sequence on the right-hand side. Sequence unpacking requires that there are as
357many variables on the left side of the equals sign as there are elements in the
Benjamin Petersond23f8222009-04-05 19:13:16 +0000358sequence. Note that multiple assignment is really just a combination of tuple
359packing and sequence unpacking.
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000360
361.. XXX Add a bit on the difference between tuples and lists.
362
363
Georg Brandl116aa622007-08-15 14:28:22 +0000364.. _tut-sets:
365
366Sets
367====
368
369Python also includes a data type for *sets*. A set is an unordered collection
370with no duplicate elements. Basic uses include membership testing and
371eliminating duplicate entries. Set objects also support mathematical operations
372like union, intersection, difference, and symmetric difference.
373
Georg Brandl10e0e302009-06-08 20:25:55 +0000374Curly braces or the :func:`set` function can be use to create sets. Note: To
375create an empty set you have to use ``set()``, not ``{}``; the latter creates an
376empty dictionary, a data structure that we discuss in the next section.
Guido van Rossum0616b792007-08-31 03:25:11 +0000377
Georg Brandl116aa622007-08-15 14:28:22 +0000378Here is a brief demonstration::
379
Guido van Rossum0616b792007-08-31 03:25:11 +0000380 >>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
381 >>> print(basket)
Georg Brandl2080ea52008-12-07 15:17:53 +0000382 {'orange', 'banana', 'pear', 'apple'}
Guido van Rossum0616b792007-08-31 03:25:11 +0000383 >>> fruit = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
Georg Brandl116aa622007-08-15 14:28:22 +0000384 >>> fruit = set(basket) # create a set without duplicates
385 >>> fruit
Guido van Rossum0616b792007-08-31 03:25:11 +0000386 {'orange', 'pear', 'apple', 'banana'}
Georg Brandlf6945182008-02-01 11:56:49 +0000387 >>> fruit = {'orange', 'apple'} # {} syntax is equivalent to [] for lists
388 >>> fruit
389 {'orange', 'apple'}
Georg Brandl116aa622007-08-15 14:28:22 +0000390 >>> 'orange' in fruit # fast membership testing
391 True
392 >>> 'crabgrass' in fruit
393 False
394
395 >>> # Demonstrate set operations on unique letters from two words
396 ...
397 >>> a = set('abracadabra')
398 >>> b = set('alacazam')
399 >>> a # unique letters in a
Guido van Rossum0616b792007-08-31 03:25:11 +0000400 {'a', 'r', 'b', 'c', 'd'}
Georg Brandl116aa622007-08-15 14:28:22 +0000401 >>> a - b # letters in a but not in b
Guido van Rossum0616b792007-08-31 03:25:11 +0000402 {'r', 'd', 'b'}
Georg Brandl116aa622007-08-15 14:28:22 +0000403 >>> a | b # letters in either a or b
Guido van Rossum0616b792007-08-31 03:25:11 +0000404 {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
Georg Brandl116aa622007-08-15 14:28:22 +0000405 >>> a & b # letters in both a and b
Guido van Rossum0616b792007-08-31 03:25:11 +0000406 {'a', 'c'}
Georg Brandl116aa622007-08-15 14:28:22 +0000407 >>> a ^ b # letters in a or b but not both
Guido van Rossum0616b792007-08-31 03:25:11 +0000408 {'r', 'd', 'b', 'm', 'z', 'l'}
409
Georg Brandlfc11f272009-06-16 19:22:10 +0000410Like :ref:`for lists <tut-listcomps>`, there is a set comprehension syntax::
Georg Brandlf6945182008-02-01 11:56:49 +0000411
412 >>> a = {x for x in 'abracadabra' if x not in 'abc'}
413 >>> a
414 {'r', 'd'}
Guido van Rossum0616b792007-08-31 03:25:11 +0000415
Georg Brandl116aa622007-08-15 14:28:22 +0000416
417
418.. _tut-dictionaries:
419
420Dictionaries
421============
422
423Another useful data type built into Python is the *dictionary* (see
424:ref:`typesmapping`). Dictionaries are sometimes found in other languages as
425"associative memories" or "associative arrays". Unlike sequences, which are
426indexed by a range of numbers, dictionaries are indexed by *keys*, which can be
427any immutable type; strings and numbers can always be keys. Tuples can be used
428as keys if they contain only strings, numbers, or tuples; if a tuple contains
429any mutable object either directly or indirectly, it cannot be used as a key.
430You can't use lists as keys, since lists can be modified in place using index
431assignments, slice assignments, or methods like :meth:`append` and
432:meth:`extend`.
433
434It is best to think of a dictionary as an unordered set of *key: value* pairs,
435with the requirement that the keys are unique (within one dictionary). A pair of
436braces creates an empty dictionary: ``{}``. Placing a comma-separated list of
437key:value pairs within the braces adds initial key:value pairs to the
438dictionary; this is also the way dictionaries are written on output.
439
440The main operations on a dictionary are storing a value with some key and
441extracting the value given the key. It is also possible to delete a key:value
442pair with ``del``. If you store using a key that is already in use, the old
443value associated with that key is forgotten. It is an error to extract a value
444using a non-existent key.
445
Georg Brandlabffe712008-12-15 08:28:37 +0000446Performing ``list(d.keys())`` on a dictionary returns a list of all the keys
Georg Brandlfc11f272009-06-16 19:22:10 +0000447used in the dictionary, in arbitrary order (if you want it sorted, just use
448``sorted(d.keys())`` instead). [1]_ To check whether a single key is in the
449dictionary, use the :keyword:`in` keyword.
Georg Brandl116aa622007-08-15 14:28:22 +0000450
451Here is a small example using a dictionary::
452
453 >>> tel = {'jack': 4098, 'sape': 4139}
454 >>> tel['guido'] = 4127
455 >>> tel
456 {'sape': 4139, 'guido': 4127, 'jack': 4098}
457 >>> tel['jack']
458 4098
459 >>> del tel['sape']
460 >>> tel['irv'] = 4127
461 >>> tel
462 {'guido': 4127, 'irv': 4127, 'jack': 4098}
Neal Norwitze0906d12007-08-31 03:46:28 +0000463 >>> list(tel.keys())
Georg Brandlabffe712008-12-15 08:28:37 +0000464 ['irv', 'guido', 'jack']
465 >>> sorted(tel.keys())
Georg Brandl116aa622007-08-15 14:28:22 +0000466 ['guido', 'irv', 'jack']
Georg Brandl116aa622007-08-15 14:28:22 +0000467 >>> 'guido' in tel
468 True
Neal Norwitze0906d12007-08-31 03:46:28 +0000469 >>> 'jack' not in tel
470 False
Georg Brandl116aa622007-08-15 14:28:22 +0000471
Georg Brandlfc11f272009-06-16 19:22:10 +0000472The :func:`dict` constructor builds dictionaries directly from sequences of
Raymond Hettinger8699aea2009-06-16 20:49:30 +0000473key-value pairs::
Georg Brandl116aa622007-08-15 14:28:22 +0000474
475 >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
476 {'sape': 4139, 'jack': 4098, 'guido': 4127}
Georg Brandl116aa622007-08-15 14:28:22 +0000477
Georg Brandlf6945182008-02-01 11:56:49 +0000478In addition, dict comprehensions can be used to create dictionaries from
479arbitrary key and value expressions::
480
481 >>> {x: x**2 for x in (2, 4, 6)}
482 {2: 4, 4: 16, 6: 36}
Georg Brandl116aa622007-08-15 14:28:22 +0000483
484When the keys are simple strings, it is sometimes easier to specify pairs using
485keyword arguments::
486
487 >>> dict(sape=4139, guido=4127, jack=4098)
488 {'sape': 4139, 'jack': 4098, 'guido': 4127}
489
490
491.. _tut-loopidioms:
492
493Looping Techniques
494==================
495
496When looping through dictionaries, the key and corresponding value can be
Neal Norwitze0906d12007-08-31 03:46:28 +0000497retrieved at the same time using the :meth:`items` method. ::
Georg Brandl116aa622007-08-15 14:28:22 +0000498
499 >>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
Neal Norwitze0906d12007-08-31 03:46:28 +0000500 >>> for k, v in knights.items():
Guido van Rossum0616b792007-08-31 03:25:11 +0000501 ... print(k, v)
Georg Brandl116aa622007-08-15 14:28:22 +0000502 ...
503 gallahad the pure
504 robin the brave
505
506When looping through a sequence, the position index and corresponding value can
507be retrieved at the same time using the :func:`enumerate` function. ::
508
509 >>> for i, v in enumerate(['tic', 'tac', 'toe']):
Guido van Rossum0616b792007-08-31 03:25:11 +0000510 ... print(i, v)
Georg Brandl116aa622007-08-15 14:28:22 +0000511 ...
512 0 tic
513 1 tac
514 2 toe
515
516To loop over two or more sequences at the same time, the entries can be paired
517with the :func:`zip` function. ::
518
519 >>> questions = ['name', 'quest', 'favorite color']
520 >>> answers = ['lancelot', 'the holy grail', 'blue']
521 >>> for q, a in zip(questions, answers):
Benjamin Petersone6f00632008-05-26 01:03:56 +0000522 ... print('What is your {0}? It is {1}.'.format(q, a))
Georg Brandl06788c92009-01-03 21:31:47 +0000523 ...
Georg Brandl116aa622007-08-15 14:28:22 +0000524 What is your name? It is lancelot.
525 What is your quest? It is the holy grail.
526 What is your favorite color? It is blue.
527
528To loop over a sequence in reverse, first specify the sequence in a forward
529direction and then call the :func:`reversed` function. ::
530
Georg Brandle4ac7502007-09-03 07:10:24 +0000531 >>> for i in reversed(range(1, 10, 2)):
Guido van Rossum0616b792007-08-31 03:25:11 +0000532 ... print(i)
Georg Brandl116aa622007-08-15 14:28:22 +0000533 ...
534 9
535 7
536 5
537 3
538 1
539
540To loop over a sequence in sorted order, use the :func:`sorted` function which
541returns a new sorted list while leaving the source unaltered. ::
542
543 >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
544 >>> for f in sorted(set(basket)):
Guido van Rossum0616b792007-08-31 03:25:11 +0000545 ... print(f)
Georg Brandl06788c92009-01-03 21:31:47 +0000546 ...
Georg Brandl116aa622007-08-15 14:28:22 +0000547 apple
548 banana
549 orange
550 pear
551
552
553.. _tut-conditions:
554
555More on Conditions
556==================
557
558The conditions used in ``while`` and ``if`` statements can contain any
559operators, not just comparisons.
560
561The comparison operators ``in`` and ``not in`` check whether a value occurs
562(does not occur) in a sequence. The operators ``is`` and ``is not`` compare
563whether two objects are really the same object; this only matters for mutable
564objects like lists. All comparison operators have the same priority, which is
565lower than that of all numerical operators.
566
567Comparisons can be chained. For example, ``a < b == c`` tests whether ``a`` is
568less than ``b`` and moreover ``b`` equals ``c``.
569
570Comparisons may be combined using the Boolean operators ``and`` and ``or``, and
571the outcome of a comparison (or of any other Boolean expression) may be negated
572with ``not``. These have lower priorities than comparison operators; between
573them, ``not`` has the highest priority and ``or`` the lowest, so that ``A and
574not B or C`` is equivalent to ``(A and (not B)) or C``. As always, parentheses
575can be used to express the desired composition.
576
577The Boolean operators ``and`` and ``or`` are so-called *short-circuit*
578operators: their arguments are evaluated from left to right, and evaluation
579stops as soon as the outcome is determined. For example, if ``A`` and ``C`` are
580true but ``B`` is false, ``A and B and C`` does not evaluate the expression
581``C``. When used as a general value and not as a Boolean, the return value of a
582short-circuit operator is the last evaluated argument.
583
584It is possible to assign the result of a comparison or other Boolean expression
585to a variable. For example, ::
586
587 >>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
588 >>> non_null = string1 or string2 or string3
589 >>> non_null
590 'Trondheim'
591
592Note that in Python, unlike C, assignment cannot occur inside expressions. C
593programmers may grumble about this, but it avoids a common class of problems
594encountered in C programs: typing ``=`` in an expression when ``==`` was
595intended.
596
597
598.. _tut-comparing:
599
600Comparing Sequences and Other Types
601===================================
602
603Sequence objects may be compared to other objects with the same sequence type.
604The comparison uses *lexicographical* ordering: first the first two items are
605compared, and if they differ this determines the outcome of the comparison; if
606they are equal, the next two items are compared, and so on, until either
607sequence is exhausted. If two items to be compared are themselves sequences of
608the same type, the lexicographical comparison is carried out recursively. If
609all items of two sequences compare equal, the sequences are considered equal.
610If one sequence is an initial sub-sequence of the other, the shorter sequence is
Georg Brandlfc11f272009-06-16 19:22:10 +0000611the smaller (lesser) one. Lexicographical ordering for strings uses the Unicode
612codepoint number to order individual characters. Some examples of comparisons
613between sequences of the same type::
Georg Brandl116aa622007-08-15 14:28:22 +0000614
615 (1, 2, 3) < (1, 2, 4)
616 [1, 2, 3] < [1, 2, 4]
617 'ABC' < 'C' < 'Pascal' < 'Python'
618 (1, 2, 3, 4) < (1, 2, 4)
619 (1, 2) < (1, 2, -1)
620 (1, 2, 3) == (1.0, 2.0, 3.0)
621 (1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
622
Georg Brandl9f2c39a2007-10-08 14:08:36 +0000623Note that comparing objects of different types with ``<`` or ``>`` is legal
624provided that the objects have appropriate comparison methods. For example,
625mixed numeric types are compared according to their numeric value, so 0 equals
6260.0, etc. Otherwise, rather than providing an arbitrary ordering, the
627interpreter will raise a :exc:`TypeError` exception.
Georg Brandlfc11f272009-06-16 19:22:10 +0000628
629
630.. rubric:: Footnotes
631
632.. [1] Calling ``d.keys()`` will return a :dfn:`dictionary view` object. It
633 supports operations like membership test and iteration, but its contents
634 are not independent of the original dictionary -- it is only a *view*.