annotate Doc/tutorial/datastructures.rst @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 2e2e7f398864
children c2e4ab7ed26e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
1 .. _tut-structures:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
2
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
3 ***************
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
4 Data Structures
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
5 ***************
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
6
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
7 This chapter describes some things you've learned about already in more detail,
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
8 and adds some new things as well.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
9
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
10
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
11 .. _tut-morelists:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
12
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
13 More on Lists
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
14 =============
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
15
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
16 The list data type has some more methods. Here are all of the methods of list
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
17 objects:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
18
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
19
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
20 .. method:: list.append(x)
45972
c45a45669017 Mark the descitems in the tutorial as "noindex" so that :meth: cross-refs don't link to them.
Georg Brandl <georg@python.org>
parents: 44383
diff changeset
21 :noindex:
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
22
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
23 Add an item to the end of the list; equivalent to ``a[len(a):] = [x]``.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
24
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
25
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
26 .. method:: list.extend(L)
45972
c45a45669017 Mark the descitems in the tutorial as "noindex" so that :meth: cross-refs don't link to them.
Georg Brandl <georg@python.org>
parents: 44383
diff changeset
27 :noindex:
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
28
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
29 Extend the list by appending all the items in the given list; equivalent to
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
30 ``a[len(a):] = L``.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
31
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
32
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
33 .. method:: list.insert(i, x)
45972
c45a45669017 Mark the descitems in the tutorial as "noindex" so that :meth: cross-refs don't link to them.
Georg Brandl <georg@python.org>
parents: 44383
diff changeset
34 :noindex:
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
35
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
36 Insert an item at a given position. The first argument is the index of the
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
37 element before which to insert, so ``a.insert(0, x)`` inserts at the front of
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
38 the list, and ``a.insert(len(a), x)`` is equivalent to ``a.append(x)``.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
39
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
40
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
41 .. method:: list.remove(x)
45972
c45a45669017 Mark the descitems in the tutorial as "noindex" so that :meth: cross-refs don't link to them.
Georg Brandl <georg@python.org>
parents: 44383
diff changeset
42 :noindex:
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
43
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
44 Remove the first item from the list whose value is *x*. It is an error if there
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
45 is no such item.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
46
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
47
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
48 .. method:: list.pop([i])
45972
c45a45669017 Mark the descitems in the tutorial as "noindex" so that :meth: cross-refs don't link to them.
Georg Brandl <georg@python.org>
parents: 44383
diff changeset
49 :noindex:
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
50
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
51 Remove the item at the given position in the list, and return it. If no index
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
52 is specified, ``a.pop()`` removes and returns the last item in the list. (The
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
53 square brackets around the *i* in the method signature denote that the parameter
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
54 is optional, not that you should type square brackets at that position. You
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
55 will see this notation frequently in the Python Library Reference.)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
56
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
57
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
58 .. method:: list.index(x)
45972
c45a45669017 Mark the descitems in the tutorial as "noindex" so that :meth: cross-refs don't link to them.
Georg Brandl <georg@python.org>
parents: 44383
diff changeset
59 :noindex:
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
60
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
61 Return the index in the list of the first item whose value is *x*. It is an
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
62 error if there is no such item.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
63
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
64
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
65 .. method:: list.count(x)
45972
c45a45669017 Mark the descitems in the tutorial as "noindex" so that :meth: cross-refs don't link to them.
Georg Brandl <georg@python.org>
parents: 44383
diff changeset
66 :noindex:
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
67
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
68 Return the number of times *x* appears in the list.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
69
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
70
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
71 .. method:: list.sort()
45972
c45a45669017 Mark the descitems in the tutorial as "noindex" so that :meth: cross-refs don't link to them.
Georg Brandl <georg@python.org>
parents: 44383
diff changeset
72 :noindex:
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
73
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
74 Sort the items of the list, in place.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
75
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
76
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
77 .. method:: list.reverse()
45972
c45a45669017 Mark the descitems in the tutorial as "noindex" so that :meth: cross-refs don't link to them.
Georg Brandl <georg@python.org>
parents: 44383
diff changeset
78 :noindex:
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
79
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
80 Reverse the elements of the list, in place.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
81
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
82 An example that uses most of the list methods::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
83
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
84 >>> a = [66.25, 333, 333, 1, 1234.5]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
85 >>> print a.count(333), a.count(66.25), a.count('x')
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
86 2 1 0
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
87 >>> a.insert(2, -1)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
88 >>> a.append(333)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
89 >>> a
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
90 [66.25, 333, -1, 333, 1, 1234.5, 333]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
91 >>> a.index(333)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
92 1
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
93 >>> a.remove(333)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
94 >>> a
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
95 [66.25, -1, 333, 1, 1234.5, 333]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
96 >>> a.reverse()
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
97 >>> a
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
98 [333, 1234.5, 1, 333, -1, 66.25]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
99 >>> a.sort()
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
100 >>> a
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
101 [-1, 1, 66.25, 333, 333, 1234.5]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
102
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
103
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
104 .. _tut-lists-as-stacks:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
105
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
106 Using Lists as Stacks
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
107 ---------------------
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
108
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
109 .. sectionauthor:: Ka-Ping Yee <ping@lfw.org>
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
110
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
111
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
112 The list methods make it very easy to use a list as a stack, where the last
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
113 element added is the first element retrieved ("last-in, first-out"). To add an
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
114 item to the top of the stack, use :meth:`append`. To retrieve an item from the
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
115 top of the stack, use :meth:`pop` without an explicit index. For example::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
116
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
117 >>> stack = [3, 4, 5]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
118 >>> stack.append(6)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
119 >>> stack.append(7)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
120 >>> stack
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
121 [3, 4, 5, 6, 7]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
122 >>> stack.pop()
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
123 7
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
124 >>> stack
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
125 [3, 4, 5, 6]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
126 >>> stack.pop()
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
127 6
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
128 >>> stack.pop()
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
129 5
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
130 >>> stack
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
131 [3, 4]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
132
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
133
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
134 .. _tut-lists-as-queues:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
135
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
136 Using Lists as Queues
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
137 ---------------------
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
138
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
139 .. sectionauthor:: Ka-Ping Yee <ping@lfw.org>
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
140
56655
5d8b2d6dcd62 Update advice on how to implement a queue.
Raymond Hettinger <python@rcn.com>
parents: 55049
diff changeset
141 It is also possible to use a list as a queue, where the first element added is
5d8b2d6dcd62 Update advice on how to implement a queue.
Raymond Hettinger <python@rcn.com>
parents: 55049
diff changeset
142 the first element retrieved ("first-in, first-out"); however, lists are not
5d8b2d6dcd62 Update advice on how to implement a queue.
Raymond Hettinger <python@rcn.com>
parents: 55049
diff changeset
143 efficient for this purpose. While appends and pops from the end of list are
5d8b2d6dcd62 Update advice on how to implement a queue.
Raymond Hettinger <python@rcn.com>
parents: 55049
diff changeset
144 fast, doing inserts or pops from the beginning of a list is slow (because all
5d8b2d6dcd62 Update advice on how to implement a queue.
Raymond Hettinger <python@rcn.com>
parents: 55049
diff changeset
145 of the other elements have to be shifted by one).
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
146
56655
5d8b2d6dcd62 Update advice on how to implement a queue.
Raymond Hettinger <python@rcn.com>
parents: 55049
diff changeset
147 To implement a queue, use :class:`collections.deque` which was designed to
5d8b2d6dcd62 Update advice on how to implement a queue.
Raymond Hettinger <python@rcn.com>
parents: 55049
diff changeset
148 have fast appends and pops from both ends. For example::
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
149
56655
5d8b2d6dcd62 Update advice on how to implement a queue.
Raymond Hettinger <python@rcn.com>
parents: 55049
diff changeset
150 >>> from collections import deque
5d8b2d6dcd62 Update advice on how to implement a queue.
Raymond Hettinger <python@rcn.com>
parents: 55049
diff changeset
151 >>> queue = deque(["Eric", "John", "Michael"])
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
152 >>> queue.append("Terry") # Terry arrives
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
153 >>> queue.append("Graham") # Graham arrives
56655
5d8b2d6dcd62 Update advice on how to implement a queue.
Raymond Hettinger <python@rcn.com>
parents: 55049
diff changeset
154 >>> queue.popleft() # The first to arrive now leaves
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
155 'Eric'
56655
5d8b2d6dcd62 Update advice on how to implement a queue.
Raymond Hettinger <python@rcn.com>
parents: 55049
diff changeset
156 >>> queue.popleft() # The second to arrive now leaves
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
157 'John'
56655
5d8b2d6dcd62 Update advice on how to implement a queue.
Raymond Hettinger <python@rcn.com>
parents: 55049
diff changeset
158 >>> queue # Remaining queue in order of arrival
5d8b2d6dcd62 Update advice on how to implement a queue.
Raymond Hettinger <python@rcn.com>
parents: 55049
diff changeset
159 deque(['Michael', 'Terry', 'Graham'])
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
160
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
161
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
162 .. _tut-functional:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
163
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
164 Functional Programming Tools
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
165 ----------------------------
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
166
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
167 There are three built-in functions that are very useful when used with lists:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
168 :func:`filter`, :func:`map`, and :func:`reduce`.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
169
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
170 ``filter(function, sequence)`` returns a sequence consisting of those items from
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
171 the sequence for which ``function(item)`` is true. If *sequence* is a
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
172 :class:`string` or :class:`tuple`, the result will be of the same type;
72513
f612b783bc17 Doc fix. Mathematically correct sentence.
Senthil Kumaran <senthil@uthcode.com>
parents: 68256
diff changeset
173 otherwise, it is always a :class:`list`. For example, to compute a sequence of
f612b783bc17 Doc fix. Mathematically correct sentence.
Senthil Kumaran <senthil@uthcode.com>
parents: 68256
diff changeset
174 numbers not divisible by 2 and 3::
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
175
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
176 >>> def f(x): return x % 2 != 0 and x % 3 != 0
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
177 ...
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
178 >>> filter(f, range(2, 25))
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
179 [5, 7, 11, 13, 17, 19, 23]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
180
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
181 ``map(function, sequence)`` calls ``function(item)`` for each of the sequence's
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
182 items and returns a list of the return values. For example, to compute some
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
183 cubes::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
184
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
185 >>> def cube(x): return x*x*x
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
186 ...
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
187 >>> map(cube, range(1, 11))
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
188 [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
189
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
190 More than one sequence may be passed; the function must then have as many
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
191 arguments as there are sequences and is called with the corresponding item from
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
192 each sequence (or ``None`` if some sequence is shorter than another). For
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
193 example::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
194
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
195 >>> seq = range(8)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
196 >>> def add(x, y): return x+y
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
197 ...
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
198 >>> map(add, seq, seq)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
199 [0, 2, 4, 6, 8, 10, 12, 14]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
200
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
201 ``reduce(function, sequence)`` returns a single value constructed by calling the
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
202 binary function *function* on the first two items of the sequence, then on the
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
203 result and the next item, and so on. For example, to compute the sum of the
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
204 numbers 1 through 10::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
205
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
206 >>> def add(x,y): return x+y
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
207 ...
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
208 >>> reduce(add, range(1, 11))
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
209 55
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
210
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
211 If there's only one item in the sequence, its value is returned; if the sequence
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
212 is empty, an exception is raised.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
213
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
214 A third argument can be passed to indicate the starting value. In this case the
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
215 starting value is returned for an empty sequence, and the function is first
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
216 applied to the starting value and the first sequence item, then to the result
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
217 and the next item, and so on. For example, ::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
218
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
219 >>> def sum(seq):
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
220 ... def add(x,y): return x+y
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
221 ... return reduce(add, seq, 0)
50522
ee9ced92843a Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49182
diff changeset
222 ...
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
223 >>> sum(range(1, 11))
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
224 55
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
225 >>> sum([])
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
226 0
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
227
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
228 Don't use this example's definition of :func:`sum`: since summing numbers is
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
229 such a common need, a built-in function ``sum(sequence)`` is already provided,
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
230 and works exactly like this.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
231
80459
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
232 .. _tut-listcomps:
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
233
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
234 List Comprehensions
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
235 -------------------
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
236
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
237 List comprehensions provide a concise way to create lists.
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
238 Common applications are to make new lists where each element is the result of
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
239 some operations applied to each member of another sequence or iterable, or to
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
240 create a subsequence of those elements that satisfy a certain condition.
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
241
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
242 For example, assume we want to create a list of squares, like::
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
243
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
244 >>> squares = []
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
245 >>> for x in range(10):
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
246 ... squares.append(x**2)
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
247 ...
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
248 >>> squares
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
249 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
250
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
251 We can obtain the same result with::
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
252
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
253 squares = [x**2 for x in range(10)]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
254
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
255 This is also equivalent to ``squares = map(lambda x: x**2, range(10))``,
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
256 but it's more concise and readable.
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
257
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
258 A list comprehension consists of brackets containing an expression followed
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
259 by a :keyword:`for` clause, then zero or more :keyword:`for` or :keyword:`if`
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
260 clauses. The result will be a new list resulting from evaluating the expression
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
261 in the context of the :keyword:`for` and :keyword:`if` clauses which follow it.
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
262 For example, this listcomp combines the elements of two lists if they are not
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
263 equal::
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
264
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
265 >>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
266 [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
267
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
268 and it's equivalent to:
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
269
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
270 >>> combs = []
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
271 >>> for x in [1,2,3]:
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
272 ... for y in [3,1,4]:
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
273 ... if x != y:
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
274 ... combs.append((x, y))
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
275 ...
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
276 >>> combs
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
277 [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
278
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
279 Note how the order of the :keyword:`for` and :keyword:`if` statements is the
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
280 same in both these snippets.
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
281
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
282 If the expression is a tuple (e.g. the ``(x, y)`` in the previous example),
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
283 it must be parenthesized. ::
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
284
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
285 >>> vec = [-4, -2, 0, 2, 4]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
286 >>> # create a new list with the values doubled
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
287 >>> [x*2 for x in vec]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
288 [-8, -4, 0, 4, 8]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
289 >>> # filter the list to exclude negative numbers
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
290 >>> [x for x in vec if x >= 0]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
291 [0, 2, 4]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
292 >>> # apply a function to all the elements
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
293 >>> [abs(x) for x in vec]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
294 [4, 2, 0, 2, 4]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
295 >>> # call a method on each element
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
296 >>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
297 >>> [weapon.strip() for weapon in freshfruit]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
298 ['banana', 'loganberry', 'passion fruit']
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
299 >>> # create a list of 2-tuples like (number, square)
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
300 >>> [(x, x**2) for x in range(6)]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
301 [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
302 >>> # the tuple must be parenthesized, otherwise an error is raised
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
303 >>> [x, x**2 for x in range(6)]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
304 File "<stdin>", line 1
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
305 [x, x**2 for x in range(6)]
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
306 ^
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
307 SyntaxError: invalid syntax
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
308 >>> # flatten a list using a listcomp with two 'for'
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
309 >>> vec = [[1,2,3], [4,5,6], [7,8,9]]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
310 >>> [num for elem in vec for num in elem]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
311 [1, 2, 3, 4, 5, 6, 7, 8, 9]
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
312
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
313 List comprehensions can contain complex expressions and nested functions::
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
314
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
315 >>> from math import pi
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
316 >>> [str(round(pi, i)) for i in range(1, 6)]
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
317 ['3.1', '3.14', '3.142', '3.1416', '3.14159']
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
318
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
319
44302
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
320 Nested List Comprehensions
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
321 ''''''''''''''''''''''''''
44302
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
322
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
323 The initial expression in a list comprehension can be any arbitrary expression,
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
324 including another list comprehension.
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
325
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
326 Consider the following example of a 3x4 matrix implemented as a list of
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
327 3 lists of length 4::
44302
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
328
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
329 >>> matrix = [
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
330 ... [1, 2, 3, 4],
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
331 ... [5, 6, 7, 8],
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
332 ... [9, 10, 11, 12],
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
333 ... ]
44302
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
334
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
335 The following list comprehension will transpose rows and columns::
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
336
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
337 >>> [[row[i] for row in matrix] for i in range(4)]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
338 [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
339
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
340 As we saw in the previous section, the nested listcomp is evaluated in
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
341 the context of the :keyword:`for` that follows it, so this example is
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
342 equivalent to::
44302
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
343
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
344 >>> transposed = []
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
345 >>> for i in range(4):
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
346 ... transposed.append([row[i] for row in matrix])
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
347 ...
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
348 >>> transposed
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
349 [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
44302
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
350
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
351 which, in turn, is the same as::
44302
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
352
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
353 >>> transposed = []
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
354 >>> for i in range(4):
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
355 ... # the following 3 lines implement the nested listcomp
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
356 ... transposed_row = []
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
357 ... for row in matrix:
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
358 ... transposed_row.append(row[i])
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
359 ... transposed.append(transposed_row)
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
360 ...
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
361 >>> transposed
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
362 [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
44302
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
363
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
364
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
365 In the real world, you should prefer built-in functions to complex flow statements.
44302
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
366 The :func:`zip` function would do a great job for this use case::
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
367
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
368 >>> zip(*matrix)
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
369 [(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
44302
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
370
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
371 See :ref:`tut-unpacking-arguments` for details on the asterisk in this line.
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
372
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
373 .. _tut-del:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
374
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
375 The :keyword:`del` statement
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
376 ============================
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
377
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
378 There is a way to remove an item from a list given its index instead of its
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
379 value: the :keyword:`del` statement. This differs from the :meth:`pop` method
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
380 which returns a value. The :keyword:`del` statement can also be used to remove
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
381 slices from a list or clear the entire list (which we did earlier by assignment
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
382 of an empty list to the slice). For example::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
383
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
384 >>> a = [-1, 1, 66.25, 333, 333, 1234.5]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
385 >>> del a[0]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
386 >>> a
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
387 [1, 66.25, 333, 333, 1234.5]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
388 >>> del a[2:4]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
389 >>> a
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
390 [1, 66.25, 1234.5]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
391 >>> del a[:]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
392 >>> a
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
393 []
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
394
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
395 :keyword:`del` can also be used to delete entire variables::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
396
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
397 >>> del a
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
398
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
399 Referencing the name ``a`` hereafter is an error (at least until another value
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
400 is assigned to it). We'll find other uses for :keyword:`del` later.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
401
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
402
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
403 .. _tut-tuples:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
404
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
405 Tuples and Sequences
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
406 ====================
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
407
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
408 We saw that lists and strings have many common properties, such as indexing and
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
409 slicing operations. They are two examples of *sequence* data types (see
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
410 :ref:`typesseq`). Since Python is an evolving language, other sequence data
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
411 types may be added. There is also another standard sequence data type: the
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
412 *tuple*.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
413
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
414 A tuple consists of a number of values separated by commas, for instance::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
415
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
416 >>> t = 12345, 54321, 'hello!'
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
417 >>> t[0]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
418 12345
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
419 >>> t
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
420 (12345, 54321, 'hello!')
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
421 >>> # Tuples may be nested:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
422 ... u = t, (1, 2, 3, 4, 5)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
423 >>> u
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
424 ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
77495
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
425 >>> # Tuples are immutable:
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
426 ... t[0] = 88888
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
427 Traceback (most recent call last):
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
428 File "<stdin>", line 1, in <module>
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
429 TypeError: 'tuple' object does not support item assignment
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
430 >>> # but they can contain mutable objects:
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
431 ... v = ([1, 2, 3], [3, 2, 1])
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
432 >>> v
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
433 ([1, 2, 3], [3, 2, 1])
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
434
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
435
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
436 As you see, on output tuples are always enclosed in parentheses, so that nested
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
437 tuples are interpreted correctly; they may be input with or without surrounding
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
438 parentheses, although often parentheses are necessary anyway (if the tuple is
77495
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
439 part of a larger expression). It is not possible to assign to the individual
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
440 items of a tuple, however it is possible to create tuples which contain mutable
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
441 objects, such as lists.
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
442
77495
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
443 Though tuples may seem similar to lists, they are often used in different
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
444 situations and for different purposes.
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
445 Tuples are :term:`immutable`, and usually contain an heterogeneous sequence of
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
446 elements that are accessed via unpacking (see later in this section) or indexing
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
447 (or even by attribute in the case of :func:`namedtuples <collections.namedtuple>`).
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
448 Lists are :term:`mutable`, and their elements are usually homogeneous and are
bb63919cde6e #14840: Add a bit on the difference between tuples and lists. Initial patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76513
diff changeset
449 accessed by iterating over the list.
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
450
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
451 A special problem is the construction of tuples containing 0 or 1 items: the
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
452 syntax has some extra quirks to accommodate these. Empty tuples are constructed
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
453 by an empty pair of parentheses; a tuple with one item is constructed by
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
454 following a value with a comma (it is not sufficient to enclose a single value
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
455 in parentheses). Ugly, but effective. For example::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
456
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
457 >>> empty = ()
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
458 >>> singleton = 'hello', # <-- note trailing comma
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
459 >>> len(empty)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
460 0
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
461 >>> len(singleton)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
462 1
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
463 >>> singleton
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
464 ('hello',)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
465
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
466 The statement ``t = 12345, 54321, 'hello!'`` is an example of *tuple packing*:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
467 the values ``12345``, ``54321`` and ``'hello!'`` are packed together in a tuple.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
468 The reverse operation is also possible::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
469
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
470 >>> x, y, z = t
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
471
52900
4acbbb3e65e2 Merged revisions 70866-70868,70870-70871,70893,70896,70902,70905,70907,70912,70915,70927,70933,70940,70944,70954,70963,70998,71056 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50522
diff changeset
472 This is called, appropriately enough, *sequence unpacking* and works for any
4acbbb3e65e2 Merged revisions 70866-70868,70870-70871,70893,70896,70902,70905,70907,70912,70915,70927,70933,70940,70944,70954,70963,70998,71056 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50522
diff changeset
473 sequence on the right-hand side. Sequence unpacking requires the list of
4acbbb3e65e2 Merged revisions 70866-70868,70870-70871,70893,70896,70902,70905,70907,70912,70915,70927,70933,70940,70944,70954,70963,70998,71056 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50522
diff changeset
474 variables on the left to have the same number of elements as the length of the
4acbbb3e65e2 Merged revisions 70866-70868,70870-70871,70893,70896,70902,70905,70907,70912,70915,70927,70933,70940,70944,70954,70963,70998,71056 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50522
diff changeset
475 sequence. Note that multiple assignment is really just a combination of tuple
4acbbb3e65e2 Merged revisions 70866-70868,70870-70871,70893,70896,70902,70905,70907,70912,70915,70927,70933,70940,70944,70954,70963,70998,71056 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50522
diff changeset
476 packing and sequence unpacking.
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
477
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
478
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
479 .. _tut-sets:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
480
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
481 Sets
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
482 ====
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
483
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
484 Python also includes a data type for *sets*. A set is an unordered collection
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
485 with no duplicate elements. Basic uses include membership testing and
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
486 eliminating duplicate entries. Set objects also support mathematical operations
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
487 like union, intersection, difference, and symmetric difference.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
488
80459
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
489 Curly braces or the :func:`set` function can be used to create sets. Note: to
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
490 create an empty set you have to use ``set()``, not ``{}``; the latter creates an
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
491 empty dictionary, a data structure that we discuss in the next section.
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
492
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
493 Here is a brief demonstration::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
494
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
495 >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
496 >>> fruit = set(basket) # create a set without duplicates
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
497 >>> fruit
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
498 set(['orange', 'pear', 'apple', 'banana'])
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
499 >>> 'orange' in fruit # fast membership testing
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
500 True
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
501 >>> 'crabgrass' in fruit
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
502 False
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
503
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
504 >>> # Demonstrate set operations on unique letters from two words
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
505 ...
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
506 >>> a = set('abracadabra')
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
507 >>> b = set('alacazam')
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
508 >>> a # unique letters in a
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
509 set(['a', 'r', 'b', 'c', 'd'])
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
510 >>> a - b # letters in a but not in b
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
511 set(['r', 'd', 'b'])
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
512 >>> a | b # letters in either a or b
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
513 set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
514 >>> a & b # letters in both a and b
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
515 set(['a', 'c'])
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
516 >>> a ^ b # letters in a or b but not both
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
517 set(['r', 'd', 'b', 'm', 'z', 'l'])
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
518
80459
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
519 Similarly to :ref:`list comprehensions <tut-listcomps>`, set comprehensions
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
520 are also supported::
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
521
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
522 >>> a = {x for x in 'abracadabra' if x not in 'abc'}
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
523 >>> a
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
524 set(['r', 'd'])
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
525
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
526
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
527 .. _tut-dictionaries:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
528
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
529 Dictionaries
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
530 ============
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
531
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
532 Another useful data type built into Python is the *dictionary* (see
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
533 :ref:`typesmapping`). Dictionaries are sometimes found in other languages as
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
534 "associative memories" or "associative arrays". Unlike sequences, which are
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
535 indexed by a range of numbers, dictionaries are indexed by *keys*, which can be
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
536 any immutable type; strings and numbers can always be keys. Tuples can be used
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
537 as keys if they contain only strings, numbers, or tuples; if a tuple contains
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
538 any mutable object either directly or indirectly, it cannot be used as a key.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
539 You can't use lists as keys, since lists can be modified in place using index
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
540 assignments, slice assignments, or methods like :meth:`append` and
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
541 :meth:`extend`.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
542
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
543 It is best to think of a dictionary as an unordered set of *key: value* pairs,
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
544 with the requirement that the keys are unique (within one dictionary). A pair of
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
545 braces creates an empty dictionary: ``{}``. Placing a comma-separated list of
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
546 key:value pairs within the braces adds initial key:value pairs to the
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
547 dictionary; this is also the way dictionaries are written on output.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
548
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
549 The main operations on a dictionary are storing a value with some key and
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
550 extracting the value given the key. It is also possible to delete a key:value
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
551 pair with ``del``. If you store using a key that is already in use, the old
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
552 value associated with that key is forgotten. It is an error to extract a value
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
553 using a non-existent key.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
554
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
555 The :meth:`keys` method of a dictionary object returns a list of all the keys
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
556 used in the dictionary, in arbitrary order (if you want it sorted, just apply
65360
80fe066c135a #8267: Use sorted() to get a sorted list of dict keys.
Georg Brandl <georg@python.org>
parents: 60099
diff changeset
557 the :func:`sorted` function to it). To check whether a single key is in the
80fe066c135a #8267: Use sorted() to get a sorted list of dict keys.
Georg Brandl <georg@python.org>
parents: 60099
diff changeset
558 dictionary, use the :keyword:`in` keyword.
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
559
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
560 Here is a small example using a dictionary::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
561
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
562 >>> tel = {'jack': 4098, 'sape': 4139}
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
563 >>> tel['guido'] = 4127
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
564 >>> tel
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
565 {'sape': 4139, 'guido': 4127, 'jack': 4098}
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
566 >>> tel['jack']
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
567 4098
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
568 >>> del tel['sape']
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
569 >>> tel['irv'] = 4127
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
570 >>> tel
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
571 {'guido': 4127, 'irv': 4127, 'jack': 4098}
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
572 >>> tel.keys()
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
573 ['guido', 'irv', 'jack']
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
574 >>> 'guido' in tel
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
575 True
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
576
80459
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
577 The :func:`dict` constructor builds dictionaries directly from sequences of
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
578 key-value pairs::
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
579
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
580 >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
581 {'sape': 4139, 'jack': 4098, 'guido': 4127}
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
582
80459
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
583 In addition, dict comprehensions can be used to create dictionaries from
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
584 arbitrary key and value expressions::
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
585
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
586 >>> {x: x**2 for x in (2, 4, 6)}
2e2e7f398864 #16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80439
diff changeset
587 {2: 4, 4: 16, 6: 36}
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
588
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
589 When the keys are simple strings, it is sometimes easier to specify pairs using
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
590 keyword arguments::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
591
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
592 >>> dict(sape=4139, guido=4127, jack=4098)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
593 {'sape': 4139, 'jack': 4098, 'guido': 4127}
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
594
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
595
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
596 .. _tut-loopidioms:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
597
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
598 Looping Techniques
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
599 ==================
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
600
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
601 When looping through a sequence, the position index and corresponding value can
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
602 be retrieved at the same time using the :func:`enumerate` function. ::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
604 >>> for i, v in enumerate(['tic', 'tac', 'toe']):
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
605 ... print i, v
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
606 ...
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
607 0 tic
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
608 1 tac
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
609 2 toe
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
610
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
611 To loop over two or more sequences at the same time, the entries can be paired
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
612 with the :func:`zip` function. ::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
613
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
614 >>> questions = ['name', 'quest', 'favorite color']
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
615 >>> answers = ['lancelot', 'the holy grail', 'blue']
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
616 >>> for q, a in zip(questions, answers):
47282
70f9a5db279c update the tutorial to use str.format
Benjamin Peterson <benjamin@python.org>
parents: 46163
diff changeset
617 ... print 'What is your {0}? It is {1}.'.format(q, a)
50522
ee9ced92843a Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49182
diff changeset
618 ...
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
619 What is your name? It is lancelot.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
620 What is your quest? It is the holy grail.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
621 What is your favorite color? It is blue.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
622
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
623 To loop over a sequence in reverse, first specify the sequence in a forward
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
624 direction and then call the :func:`reversed` function. ::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
625
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
626 >>> for i in reversed(xrange(1,10,2)):
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
627 ... print i
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
628 ...
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
629 9
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
630 7
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
631 5
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
632 3
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
633 1
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
634
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
635 To loop over a sequence in sorted order, use the :func:`sorted` function which
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
636 returns a new sorted list while leaving the source unaltered. ::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
637
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
638 >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
639 >>> for f in sorted(set(basket)):
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
640 ... print f
50522
ee9ced92843a Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49182
diff changeset
641 ...
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
642 apple
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
643 banana
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
644 orange
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
645 pear
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
646
76513
e2a3260f1718 Reorder the entries to put the type specific technique last.
Raymond Hettinger <python@rcn.com>
parents: 73951
diff changeset
647 When looping through dictionaries, the key and corresponding value can be
e2a3260f1718 Reorder the entries to put the type specific technique last.
Raymond Hettinger <python@rcn.com>
parents: 73951
diff changeset
648 retrieved at the same time using the :meth:`iteritems` method. ::
e2a3260f1718 Reorder the entries to put the type specific technique last.
Raymond Hettinger <python@rcn.com>
parents: 73951
diff changeset
649
e2a3260f1718 Reorder the entries to put the type specific technique last.
Raymond Hettinger <python@rcn.com>
parents: 73951
diff changeset
650 >>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
e2a3260f1718 Reorder the entries to put the type specific technique last.
Raymond Hettinger <python@rcn.com>
parents: 73951
diff changeset
651 >>> for k, v in knights.iteritems():
e2a3260f1718 Reorder the entries to put the type specific technique last.
Raymond Hettinger <python@rcn.com>
parents: 73951
diff changeset
652 ... print k, v
e2a3260f1718 Reorder the entries to put the type specific technique last.
Raymond Hettinger <python@rcn.com>
parents: 73951
diff changeset
653 ...
e2a3260f1718 Reorder the entries to put the type specific technique last.
Raymond Hettinger <python@rcn.com>
parents: 73951
diff changeset
654 gallahad the pure
e2a3260f1718 Reorder the entries to put the type specific technique last.
Raymond Hettinger <python@rcn.com>
parents: 73951
diff changeset
655 robin the brave
e2a3260f1718 Reorder the entries to put the type specific technique last.
Raymond Hettinger <python@rcn.com>
parents: 73951
diff changeset
656
79730
dc006b6212e7 Issue #16225: Backport from 3.2: Add additional note to tutorial about looping.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 77495
diff changeset
657 To change a sequence you are iterating over while inside the loop (for
dc006b6212e7 Issue #16225: Backport from 3.2: Add additional note to tutorial about looping.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 77495
diff changeset
658 example to duplicate certain items), it is recommended that you first make
dc006b6212e7 Issue #16225: Backport from 3.2: Add additional note to tutorial about looping.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 77495
diff changeset
659 a copy. Looping over a sequence does not implicitly make a copy. The slice
dc006b6212e7 Issue #16225: Backport from 3.2: Add additional note to tutorial about looping.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 77495
diff changeset
660 notation makes this especially convenient::
dc006b6212e7 Issue #16225: Backport from 3.2: Add additional note to tutorial about looping.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 77495
diff changeset
661
dc006b6212e7 Issue #16225: Backport from 3.2: Add additional note to tutorial about looping.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 77495
diff changeset
662 >>> words = ['cat', 'window', 'defenestrate']
dc006b6212e7 Issue #16225: Backport from 3.2: Add additional note to tutorial about looping.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 77495
diff changeset
663 >>> for w in words[:]: # Loop over a slice copy of the entire list.
dc006b6212e7 Issue #16225: Backport from 3.2: Add additional note to tutorial about looping.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 77495
diff changeset
664 ... if len(w) > 6:
dc006b6212e7 Issue #16225: Backport from 3.2: Add additional note to tutorial about looping.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 77495
diff changeset
665 ... words.insert(0, w)
dc006b6212e7 Issue #16225: Backport from 3.2: Add additional note to tutorial about looping.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 77495
diff changeset
666 ...
dc006b6212e7 Issue #16225: Backport from 3.2: Add additional note to tutorial about looping.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 77495
diff changeset
667 >>> words
dc006b6212e7 Issue #16225: Backport from 3.2: Add additional note to tutorial about looping.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 77495
diff changeset
668 ['defenestrate', 'cat', 'window', 'defenestrate']
dc006b6212e7 Issue #16225: Backport from 3.2: Add additional note to tutorial about looping.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents: 77495
diff changeset
669
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
670
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
671 .. _tut-conditions:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
672
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
673 More on Conditions
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
674 ==================
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
675
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
676 The conditions used in ``while`` and ``if`` statements can contain any
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
677 operators, not just comparisons.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
678
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
679 The comparison operators ``in`` and ``not in`` check whether a value occurs
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
680 (does not occur) in a sequence. The operators ``is`` and ``is not`` compare
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
681 whether two objects are really the same object; this only matters for mutable
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
682 objects like lists. All comparison operators have the same priority, which is
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
683 lower than that of all numerical operators.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
684
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
685 Comparisons can be chained. For example, ``a < b == c`` tests whether ``a`` is
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
686 less than ``b`` and moreover ``b`` equals ``c``.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
687
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
688 Comparisons may be combined using the Boolean operators ``and`` and ``or``, and
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
689 the outcome of a comparison (or of any other Boolean expression) may be negated
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
690 with ``not``. These have lower priorities than comparison operators; between
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
691 them, ``not`` has the highest priority and ``or`` the lowest, so that ``A and
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
692 not B or C`` is equivalent to ``(A and (not B)) or C``. As always, parentheses
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
693 can be used to express the desired composition.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
694
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
695 The Boolean operators ``and`` and ``or`` are so-called *short-circuit*
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
696 operators: their arguments are evaluated from left to right, and evaluation
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
697 stops as soon as the outcome is determined. For example, if ``A`` and ``C`` are
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
698 true but ``B`` is false, ``A and B and C`` does not evaluate the expression
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
699 ``C``. When used as a general value and not as a Boolean, the return value of a
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
700 short-circuit operator is the last evaluated argument.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
701
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
702 It is possible to assign the result of a comparison or other Boolean expression
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
703 to a variable. For example, ::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
704
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
705 >>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
706 >>> non_null = string1 or string2 or string3
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
707 >>> non_null
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
708 'Trondheim'
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
709
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
710 Note that in Python, unlike C, assignment cannot occur inside expressions. C
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
711 programmers may grumble about this, but it avoids a common class of problems
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
712 encountered in C programs: typing ``=`` in an expression when ``==`` was
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
713 intended.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
714
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
715
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
716 .. _tut-comparing:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
717
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
718 Comparing Sequences and Other Types
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
719 ===================================
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
720
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
721 Sequence objects may be compared to other objects with the same sequence type.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
722 The comparison uses *lexicographical* ordering: first the first two items are
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
723 compared, and if they differ this determines the outcome of the comparison; if
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
724 they are equal, the next two items are compared, and so on, until either
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
725 sequence is exhausted. If two items to be compared are themselves sequences of
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
726 the same type, the lexicographical comparison is carried out recursively. If
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
727 all items of two sequences compare equal, the sequences are considered equal.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
728 If one sequence is an initial sub-sequence of the other, the shorter sequence is
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
729 the smaller (lesser) one. Lexicographical ordering for strings uses the ASCII
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
730 ordering for individual characters. Some examples of comparisons between
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
731 sequences of the same type::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
732
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
733 (1, 2, 3) < (1, 2, 4)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
734 [1, 2, 3] < [1, 2, 4]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
735 'ABC' < 'C' < 'Pascal' < 'Python'
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
736 (1, 2, 3, 4) < (1, 2, 4)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
737 (1, 2) < (1, 2, -1)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
738 (1, 2, 3) == (1.0, 2.0, 3.0)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
739 (1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
740
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
741 Note that comparing objects of different types is legal. The outcome is
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
742 deterministic but arbitrary: the types are ordered by their name. Thus, a list
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
743 is always smaller than a string, a string is always smaller than a tuple, etc.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
744 [#]_ Mixed numeric types are compared according to their numeric value, so 0
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
745 equals 0.0, etc.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
746
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
747
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
748 .. rubric:: Footnotes
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
749
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
750 .. [#] The rules for comparing objects of different types should not be relied upon;
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
751 they may change in a future version of the language.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
752