annotate Doc/tutorial/datastructures.rst @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents bb63919cde6e
children dc006b6212e7
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
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
232 .. versionadded:: 2.3
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
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
235 List Comprehensions
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
236 -------------------
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
237
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
238 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
239 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
240 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
241 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
242
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
243 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
244
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
245 >>> squares = []
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
246 >>> for x in range(10):
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
247 ... squares.append(x**2)
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
248 ...
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
249 >>> squares
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
250 [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
251
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
252 We can obtain the same result with::
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
253
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
254 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
255
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
256 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
257 but it's more concise and readable.
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
258
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
259 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
260 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
261 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
262 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
263 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
264 equal::
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
265
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
266 >>> [(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
267 [(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
268
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
269 and it's equivalent to:
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
270
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
271 >>> combs = []
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
272 >>> for x in [1,2,3]:
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
273 ... for y in [3,1,4]:
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
274 ... if x != y:
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
275 ... combs.append((x, y))
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
276 ...
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
277 >>> combs
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
278 [(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
279
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
280 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
281 same in both these snippets.
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
282
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
283 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
284 it must be parenthesized. ::
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
285
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
286 >>> vec = [-4, -2, 0, 2, 4]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
287 >>> # 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
288 >>> [x*2 for x in vec]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
289 [-8, -4, 0, 4, 8]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
290 >>> # filter the list to exclude negative numbers
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
291 >>> [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
292 [0, 2, 4]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
293 >>> # apply a function to all the elements
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
294 >>> [abs(x) for x in vec]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
295 [4, 2, 0, 2, 4]
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
296 >>> # 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
297 >>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
298 >>> [weapon.strip() for weapon in freshfruit]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
299 ['banana', 'loganberry', 'passion fruit']
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
300 >>> # 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
301 >>> [(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
302 [(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
303 >>> # 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
304 >>> [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
305 File "<stdin>", line 1
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
306 [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
307 ^
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
308 SyntaxError: invalid syntax
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
309 >>> # 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
310 >>> 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
311 >>> [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
312 [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
313
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
314 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
315
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
316 >>> from math import pi
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
317 >>> [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
318 ['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
319
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
320
44302
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
321 Nested List Comprehensions
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
322 ''''''''''''''''''''''''''
44302
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
323
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
324 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
325 including another list comprehension.
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
326
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
327 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
328 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
329
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
330 >>> matrix = [
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
331 ... [1, 2, 3, 4],
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
332 ... [5, 6, 7, 8],
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
333 ... [9, 10, 11, 12],
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
334 ... ]
44302
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
335
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
336 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
337
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
338 >>> [[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
339 [[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
340
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
341 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
342 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
343 equivalent to::
44302
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
344
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
345 >>> transposed = []
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
346 >>> for i in range(4):
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
347 ... 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
348 ...
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
349 >>> transposed
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
350 [[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
351
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
352 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
353
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
354 >>> transposed = []
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
355 >>> for i in range(4):
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
356 ... # 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
357 ... transposed_row = []
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
358 ... for row in matrix:
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
359 ... transposed_row.append(row[i])
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
360 ... transposed.append(transposed_row)
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
361 ...
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
362 >>> transposed
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
363 [[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
364
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
365
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
366 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
367 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
368
73951
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
369 >>> zip(*matrix)
132158b287d7 #13549: improve tutorial section about listcomps.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72513
diff changeset
370 [(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
371
20ed1d415409 Add a section about nested listcomps to the tutorial.
Georg Brandl <georg@python.org>
parents: 42603
diff changeset
372 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
373
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
374 .. _tut-del:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
375
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
376 The :keyword:`del` statement
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
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
379 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
380 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
381 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
382 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
383 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
384
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
385 >>> 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
386 >>> del a[0]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
387 >>> a
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
388 [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
389 >>> del a[2:4]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
390 >>> a
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
391 [1, 66.25, 1234.5]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
392 >>> del a[:]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
393 >>> a
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
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
396 :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
397
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
398 >>> del a
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
399
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
400 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
401 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
402
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
403
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
404 .. _tut-tuples:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
405
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
406 Tuples and Sequences
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
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
409 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
410 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
411 :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
412 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
413 *tuple*.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
414
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
415 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
416
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
417 >>> t = 12345, 54321, 'hello!'
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
418 >>> t[0]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
419 12345
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
420 >>> t
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
421 (12345, 54321, 'hello!')
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
422 >>> # Tuples may be nested:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
423 ... 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
424 >>> u
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
425 ((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
426 >>> # 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
427 ... 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
428 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
429 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
430 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
431 >>> # 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
432 ... 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
433 >>> 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
434 ([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
435
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
436
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
437 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
438 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
439 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
440 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
441 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
442 objects, such as lists.
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
443
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
444 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
445 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
446 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
447 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
448 (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
449 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
450 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
451
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
452 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
453 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
454 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
455 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
456 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
457
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
458 >>> empty = ()
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
459 >>> singleton = 'hello', # <-- note trailing comma
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
460 >>> len(empty)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
461 0
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
462 >>> len(singleton)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
463 1
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
464 >>> singleton
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
465 ('hello',)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
466
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
467 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
468 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
469 The reverse operation is also possible::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
470
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
471 >>> x, y, z = t
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
472
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
473 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
474 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
475 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
476 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
477 packing and sequence unpacking.
42603
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
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
480 .. _tut-sets:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
481
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
482 Sets
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
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
485 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
486 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
487 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
488 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
489
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
490 Here is a brief demonstration::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
491
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
492 >>> 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
493 >>> 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
494 >>> fruit
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
495 set(['orange', 'pear', 'apple', 'banana'])
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
496 >>> 'orange' in fruit # fast membership testing
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
497 True
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
498 >>> 'crabgrass' in fruit
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
499 False
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
500
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
501 >>> # 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
502 ...
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
503 >>> a = set('abracadabra')
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
504 >>> b = set('alacazam')
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
505 >>> a # unique letters in a
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
506 set(['a', 'r', 'b', 'c', 'd'])
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
507 >>> 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
508 set(['r', 'd', 'b'])
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
509 >>> 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
510 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
511 >>> 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
512 set(['a', 'c'])
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
513 >>> 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
514 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
515
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
516
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
517 .. _tut-dictionaries:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
518
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
519 Dictionaries
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
520 ============
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
521
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
522 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
523 :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
524 "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
525 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
526 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
527 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
528 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
529 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
530 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
531 :meth:`extend`.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
532
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
533 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
534 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
535 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
536 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
537 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
538
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
539 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
540 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
541 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
542 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
543 using a non-existent key.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
544
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
545 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
546 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
547 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
548 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
549
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
550 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
551
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
552 >>> tel = {'jack': 4098, 'sape': 4139}
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
553 >>> tel['guido'] = 4127
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
554 >>> tel
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
555 {'sape': 4139, 'guido': 4127, 'jack': 4098}
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
556 >>> tel['jack']
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
557 4098
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
558 >>> del tel['sape']
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
559 >>> tel['irv'] = 4127
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
560 >>> tel
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
561 {'guido': 4127, 'irv': 4127, 'jack': 4098}
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
562 >>> tel.keys()
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
563 ['guido', 'irv', 'jack']
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
564 >>> 'guido' in tel
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
565 True
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
566
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
567 The :func:`dict` constructor builds dictionaries directly from lists of
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
568 key-value pairs stored as tuples. When the pairs form a pattern, list
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
569 comprehensions can compactly specify the key-value list. ::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
570
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
571 >>> 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
572 {'sape': 4139, 'jack': 4098, 'guido': 4127}
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
573 >>> dict([(x, x**2) for x in (2, 4, 6)]) # use a list comprehension
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
574 {2: 4, 4: 16, 6: 36}
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
575
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
576 Later in the tutorial, we will learn about Generator Expressions which are even
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
577 better suited for the task of supplying key-values pairs to the :func:`dict`
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
578 constructor.
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 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
581 keyword arguments::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
582
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
583 >>> 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
584 {'sape': 4139, 'jack': 4098, 'guido': 4127}
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
585
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
586
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
587 .. _tut-loopidioms:
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 Looping Techniques
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
590 ==================
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 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
593 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
594
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
595 >>> 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
596 ... print i, v
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 0 tic
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
599 1 tac
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
600 2 toe
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
601
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
602 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
603 with the :func:`zip` function. ::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
604
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
605 >>> questions = ['name', 'quest', 'favorite color']
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
606 >>> answers = ['lancelot', 'the holy grail', 'blue']
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
607 >>> 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
608 ... 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
609 ...
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
610 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
611 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
612 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
613
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
614 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
615 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
616
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
617 >>> 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
618 ... print i
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
619 ...
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
620 9
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
621 7
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
622 5
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
623 3
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
624 1
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 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
627 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
628
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
629 >>> 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
630 >>> for f in sorted(set(basket)):
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
631 ... 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
632 ...
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
633 apple
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
634 banana
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
635 orange
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
636 pear
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
637
76513
e2a3260f1718 Reorder the entries to put the type specific technique last.
Raymond Hettinger <python@rcn.com>
parents: 73951
diff changeset
638 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
639 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
640
e2a3260f1718 Reorder the entries to put the type specific technique last.
Raymond Hettinger <python@rcn.com>
parents: 73951
diff changeset
641 >>> 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
642 >>> 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
643 ... print k, v
e2a3260f1718 Reorder the entries to put the type specific technique last.
Raymond Hettinger <python@rcn.com>
parents: 73951
diff changeset
644 ...
e2a3260f1718 Reorder the entries to put the type specific technique last.
Raymond Hettinger <python@rcn.com>
parents: 73951
diff changeset
645 gallahad the pure
e2a3260f1718 Reorder the entries to put the type specific technique last.
Raymond Hettinger <python@rcn.com>
parents: 73951
diff changeset
646 robin the brave
e2a3260f1718 Reorder the entries to put the type specific technique last.
Raymond Hettinger <python@rcn.com>
parents: 73951
diff changeset
647
42603
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
648
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
649 .. _tut-conditions:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
650
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
651 More on Conditions
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
652 ==================
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
653
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
654 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
655 operators, not just comparisons.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
656
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
657 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
658 (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
659 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
660 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
661 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
662
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
663 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
664 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
665
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
666 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
667 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
668 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
669 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
670 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
671 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
672
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
673 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
674 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
675 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
676 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
677 ``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
678 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
679
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
680 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
681 to a variable. For example, ::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
682
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
683 >>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
684 >>> 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
685 >>> non_null
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
686 'Trondheim'
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 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
689 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
690 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
691 intended.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
692
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
693
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
694 .. _tut-comparing:
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
695
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
696 Comparing Sequences and Other Types
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
697 ===================================
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
698
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
699 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
700 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
701 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
702 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
703 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
704 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
705 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
706 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
707 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
708 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
709 sequences of the same type::
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
710
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
711 (1, 2, 3) < (1, 2, 4)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
712 [1, 2, 3] < [1, 2, 4]
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
713 'ABC' < 'C' < 'Pascal' < 'Python'
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
714 (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
715 (1, 2) < (1, 2, -1)
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
716 (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
717 (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
718
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
719 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
720 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
721 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
722 [#]_ 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
723 equals 0.0, etc.
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
724
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
725
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
726 .. rubric:: Footnotes
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
727
c62f6669ae22 Move the 2.6 reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
728 .. [#] 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
729 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
730