Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | .. _tut-structures: |
| 2 | |
| 3 | *************** |
| 4 | Data Structures |
| 5 | *************** |
| 6 | |
| 7 | This chapter describes some things you've learned about already in more detail, |
| 8 | and adds some new things as well. |
| 9 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | .. _tut-morelists: |
| 11 | |
| 12 | More on Lists |
| 13 | ============= |
| 14 | |
| 15 | The list data type has some more methods. Here are all of the methods of list |
| 16 | objects: |
| 17 | |
| 18 | |
| 19 | .. method:: list.append(x) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 20 | :noindex: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | |
Georg Brandl | 388349a | 2011-10-08 18:32:40 +0200 | [diff] [blame] | 22 | Add an item to the end of the list. Equivalent to ``a[len(a):] = [x]``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 | |
| 24 | |
Jim Fasarakis-Hilliard | 53c1892 | 2017-02-25 23:13:33 +0200 | [diff] [blame] | 25 | .. method:: list.extend(iterable) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 26 | :noindex: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 27 | |
Jim Fasarakis-Hilliard | 53c1892 | 2017-02-25 23:13:33 +0200 | [diff] [blame] | 28 | Extend the list by appending all the items from the iterable. Equivalent to |
| 29 | ``a[len(a):] = iterable``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | |
| 31 | |
| 32 | .. method:: list.insert(i, x) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 33 | :noindex: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | |
| 35 | Insert an item at a given position. The first argument is the index of the |
| 36 | element before which to insert, so ``a.insert(0, x)`` inserts at the front of |
| 37 | the list, and ``a.insert(len(a), x)`` is equivalent to ``a.append(x)``. |
| 38 | |
| 39 | |
| 40 | .. method:: list.remove(x) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 41 | :noindex: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 42 | |
Lysandros Nikolaou | bcd1d97 | 2018-08-03 04:45:48 +0200 | [diff] [blame] | 43 | Remove the first item from the list whose value is equal to *x*. It raises a |
Stéphane Wirtel | e483f02 | 2018-10-26 12:52:11 +0200 | [diff] [blame] | 44 | :exc:`ValueError` if there is no such item. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 45 | |
| 46 | |
| 47 | .. method:: list.pop([i]) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 48 | :noindex: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 | |
| 50 | Remove the item at the given position in the list, and return it. If no index |
| 51 | is specified, ``a.pop()`` removes and returns the last item in the list. (The |
| 52 | square brackets around the *i* in the method signature denote that the parameter |
| 53 | is optional, not that you should type square brackets at that position. You |
| 54 | will see this notation frequently in the Python Library Reference.) |
| 55 | |
| 56 | |
Georg Brandl | a12b682 | 2013-10-06 13:01:19 +0200 | [diff] [blame] | 57 | .. method:: list.clear() |
| 58 | :noindex: |
| 59 | |
| 60 | Remove all items from the list. Equivalent to ``del a[:]``. |
| 61 | |
| 62 | |
Raymond Hettinger | 5bd5b9d | 2016-11-21 15:12:54 -0800 | [diff] [blame] | 63 | .. method:: list.index(x[, start[, end]]) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 64 | :noindex: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | |
Xiang Zhang | b2d7717 | 2017-03-13 10:09:16 +0800 | [diff] [blame] | 66 | Return zero-based index in the list of the first item whose value is equal to *x*. |
Raymond Hettinger | 5bd5b9d | 2016-11-21 15:12:54 -0800 | [diff] [blame] | 67 | Raises a :exc:`ValueError` if there is no such item. |
| 68 | |
| 69 | The optional arguments *start* and *end* are interpreted as in the slice |
| 70 | notation and are used to limit the search to a particular subsequence of |
Jim Fasarakis-Hilliard | 53c1892 | 2017-02-25 23:13:33 +0200 | [diff] [blame] | 71 | the list. The returned index is computed relative to the beginning of the full |
Raymond Hettinger | 5bd5b9d | 2016-11-21 15:12:54 -0800 | [diff] [blame] | 72 | sequence rather than the *start* argument. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | .. method:: list.count(x) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 76 | :noindex: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 77 | |
| 78 | Return the number of times *x* appears in the list. |
| 79 | |
| 80 | |
Raymond Hettinger | 07e0485 | 2014-05-26 18:44:04 -0700 | [diff] [blame] | 81 | .. method:: list.sort(key=None, reverse=False) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 82 | :noindex: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | |
Raymond Hettinger | 07e0485 | 2014-05-26 18:44:04 -0700 | [diff] [blame] | 84 | Sort the items of the list in place (the arguments can be used for sort |
| 85 | customization, see :func:`sorted` for their explanation). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | .. method:: list.reverse() |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 89 | :noindex: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | |
Georg Brandl | 388349a | 2011-10-08 18:32:40 +0200 | [diff] [blame] | 91 | Reverse the elements of the list in place. |
| 92 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 93 | |
Georg Brandl | a12b682 | 2013-10-06 13:01:19 +0200 | [diff] [blame] | 94 | .. method:: list.copy() |
| 95 | :noindex: |
| 96 | |
| 97 | Return a shallow copy of the list. Equivalent to ``a[:]``. |
| 98 | |
| 99 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | An example that uses most of the list methods:: |
| 101 | |
Raymond Hettinger | 8c5e190 | 2016-11-21 16:29:50 -0800 | [diff] [blame] | 102 | >>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana'] |
| 103 | >>> fruits.count('apple') |
| 104 | 2 |
| 105 | >>> fruits.count('tangerine') |
| 106 | 0 |
| 107 | >>> fruits.index('banana') |
| 108 | 3 |
| 109 | >>> fruits.index('banana', 4) # Find next banana starting a position 4 |
| 110 | 6 |
| 111 | >>> fruits.reverse() |
| 112 | >>> fruits |
| 113 | ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange'] |
| 114 | >>> fruits.append('grape') |
| 115 | >>> fruits |
| 116 | ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape'] |
| 117 | >>> fruits.sort() |
| 118 | >>> fruits |
| 119 | ['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear'] |
| 120 | >>> fruits.pop() |
| 121 | 'pear' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 122 | |
Georg Brandl | 388349a | 2011-10-08 18:32:40 +0200 | [diff] [blame] | 123 | You might have noticed that methods like ``insert``, ``remove`` or ``sort`` that |
Terry Jan Reedy | e17de09 | 2014-05-23 00:34:12 -0400 | [diff] [blame] | 124 | only modify the list have no return value printed -- they return the default |
| 125 | ``None``. [1]_ This is a design principle for all mutable data structures in |
| 126 | Python. |
Georg Brandl | 388349a | 2011-10-08 18:32:40 +0200 | [diff] [blame] | 127 | |
Raymond Hettinger | 4109263 | 2019-08-22 09:11:35 -0700 | [diff] [blame] | 128 | Another thing you might notice is that not all data can be sorted or |
| 129 | compared. For instance, ``[None, 'hello', 10]`` doesn't sort because |
| 130 | integers can't be compared to strings and *None* can't be compared to |
| 131 | other types. Also, there are some types that don't have a defined |
| 132 | ordering relation. For example, ``3+4j < 5+7j`` isn't a valid |
| 133 | comparison. |
| 134 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 135 | |
| 136 | .. _tut-lists-as-stacks: |
| 137 | |
| 138 | Using Lists as Stacks |
| 139 | --------------------- |
| 140 | |
| 141 | .. sectionauthor:: Ka-Ping Yee <ping@lfw.org> |
| 142 | |
| 143 | |
| 144 | The list methods make it very easy to use a list as a stack, where the last |
| 145 | element added is the first element retrieved ("last-in, first-out"). To add an |
| 146 | item to the top of the stack, use :meth:`append`. To retrieve an item from the |
| 147 | top of the stack, use :meth:`pop` without an explicit index. For example:: |
| 148 | |
| 149 | >>> stack = [3, 4, 5] |
| 150 | >>> stack.append(6) |
| 151 | >>> stack.append(7) |
| 152 | >>> stack |
| 153 | [3, 4, 5, 6, 7] |
| 154 | >>> stack.pop() |
| 155 | 7 |
| 156 | >>> stack |
| 157 | [3, 4, 5, 6] |
| 158 | >>> stack.pop() |
| 159 | 6 |
| 160 | >>> stack.pop() |
| 161 | 5 |
| 162 | >>> stack |
| 163 | [3, 4] |
| 164 | |
| 165 | |
| 166 | .. _tut-lists-as-queues: |
| 167 | |
| 168 | Using Lists as Queues |
| 169 | --------------------- |
| 170 | |
| 171 | .. sectionauthor:: Ka-Ping Yee <ping@lfw.org> |
| 172 | |
Ezio Melotti | 8f8db14 | 2010-03-31 07:45:32 +0000 | [diff] [blame] | 173 | It is also possible to use a list as a queue, where the first element added is |
| 174 | the first element retrieved ("first-in, first-out"); however, lists are not |
| 175 | efficient for this purpose. While appends and pops from the end of list are |
| 176 | fast, doing inserts or pops from the beginning of a list is slow (because all |
| 177 | of the other elements have to be shifted by one). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 178 | |
Ezio Melotti | 8f8db14 | 2010-03-31 07:45:32 +0000 | [diff] [blame] | 179 | To implement a queue, use :class:`collections.deque` which was designed to |
| 180 | have fast appends and pops from both ends. For example:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 181 | |
Ezio Melotti | 8f8db14 | 2010-03-31 07:45:32 +0000 | [diff] [blame] | 182 | >>> from collections import deque |
| 183 | >>> queue = deque(["Eric", "John", "Michael"]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 184 | >>> queue.append("Terry") # Terry arrives |
| 185 | >>> queue.append("Graham") # Graham arrives |
Ezio Melotti | 8f8db14 | 2010-03-31 07:45:32 +0000 | [diff] [blame] | 186 | >>> queue.popleft() # The first to arrive now leaves |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 187 | 'Eric' |
Ezio Melotti | 8f8db14 | 2010-03-31 07:45:32 +0000 | [diff] [blame] | 188 | >>> queue.popleft() # The second to arrive now leaves |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 189 | 'John' |
Ezio Melotti | 8f8db14 | 2010-03-31 07:45:32 +0000 | [diff] [blame] | 190 | >>> queue # Remaining queue in order of arrival |
| 191 | deque(['Michael', 'Terry', 'Graham']) |
Georg Brandl | 718ce2c | 2010-03-21 09:51:44 +0000 | [diff] [blame] | 192 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 193 | |
Georg Brandl | fc11f27 | 2009-06-16 19:22:10 +0000 | [diff] [blame] | 194 | .. _tut-listcomps: |
| 195 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 196 | List Comprehensions |
| 197 | ------------------- |
| 198 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 199 | List comprehensions provide a concise way to create lists. |
| 200 | Common applications are to make new lists where each element is the result of |
| 201 | some operations applied to each member of another sequence or iterable, or to |
| 202 | create a subsequence of those elements that satisfy a certain condition. |
| 203 | |
| 204 | For example, assume we want to create a list of squares, like:: |
| 205 | |
| 206 | >>> squares = [] |
| 207 | >>> for x in range(10): |
| 208 | ... squares.append(x**2) |
| 209 | ... |
| 210 | >>> squares |
| 211 | [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |
| 212 | |
R David Murray | 6bd6860 | 2014-09-30 21:25:38 -0400 | [diff] [blame] | 213 | Note that this creates (or overwrites) a variable named ``x`` that still exists |
| 214 | after the loop completes. We can calculate the list of squares without any |
| 215 | side effects using:: |
| 216 | |
| 217 | squares = list(map(lambda x: x**2, range(10))) |
| 218 | |
| 219 | or, equivalently:: |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 220 | |
| 221 | squares = [x**2 for x in range(10)] |
| 222 | |
R David Murray | 6bd6860 | 2014-09-30 21:25:38 -0400 | [diff] [blame] | 223 | which is more concise and readable. |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 224 | |
Georg Brandl | 7ae90dd | 2009-06-08 18:59:09 +0000 | [diff] [blame] | 225 | A list comprehension consists of brackets containing an expression followed |
Serhiy Storchaka | 2b57c43 | 2018-12-19 08:09:46 +0200 | [diff] [blame] | 226 | by a :keyword:`!for` clause, then zero or more :keyword:`!for` or :keyword:`!if` |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 227 | clauses. The result will be a new list resulting from evaluating the expression |
Serhiy Storchaka | 2b57c43 | 2018-12-19 08:09:46 +0200 | [diff] [blame] | 228 | in the context of the :keyword:`!for` and :keyword:`!if` clauses which follow it. |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 229 | For example, this listcomp combines the elements of two lists if they are not |
| 230 | equal:: |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 231 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 232 | >>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] |
| 233 | [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)] |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 234 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 235 | and it's equivalent to:: |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 236 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 237 | >>> combs = [] |
| 238 | >>> for x in [1,2,3]: |
| 239 | ... for y in [3,1,4]: |
| 240 | ... if x != y: |
| 241 | ... combs.append((x, y)) |
| 242 | ... |
| 243 | >>> combs |
| 244 | [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)] |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 245 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 246 | Note how the order of the :keyword:`for` and :keyword:`if` statements is the |
| 247 | same in both these snippets. |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 248 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 249 | If the expression is a tuple (e.g. the ``(x, y)`` in the previous example), |
| 250 | it must be parenthesized. :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 251 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 252 | >>> vec = [-4, -2, 0, 2, 4] |
| 253 | >>> # create a new list with the values doubled |
| 254 | >>> [x*2 for x in vec] |
| 255 | [-8, -4, 0, 4, 8] |
| 256 | >>> # filter the list to exclude negative numbers |
| 257 | >>> [x for x in vec if x >= 0] |
| 258 | [0, 2, 4] |
| 259 | >>> # apply a function to all the elements |
| 260 | >>> [abs(x) for x in vec] |
| 261 | [4, 2, 0, 2, 4] |
| 262 | >>> # call a method on each element |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 263 | >>> freshfruit = [' banana', ' loganberry ', 'passion fruit '] |
| 264 | >>> [weapon.strip() for weapon in freshfruit] |
| 265 | ['banana', 'loganberry', 'passion fruit'] |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 266 | >>> # create a list of 2-tuples like (number, square) |
| 267 | >>> [(x, x**2) for x in range(6)] |
| 268 | [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)] |
| 269 | >>> # the tuple must be parenthesized, otherwise an error is raised |
| 270 | >>> [x, x**2 for x in range(6)] |
UltimateCoder | 8856940 | 2017-05-03 22:16:45 +0530 | [diff] [blame] | 271 | File "<stdin>", line 1, in <module> |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 272 | [x, x**2 for x in range(6)] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 273 | ^ |
| 274 | SyntaxError: invalid syntax |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 275 | >>> # flatten a list using a listcomp with two 'for' |
| 276 | >>> vec = [[1,2,3], [4,5,6], [7,8,9]] |
| 277 | >>> [num for elem in vec for num in elem] |
| 278 | [1, 2, 3, 4, 5, 6, 7, 8, 9] |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 279 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 280 | List comprehensions can contain complex expressions and nested functions:: |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 281 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 282 | >>> from math import pi |
| 283 | >>> [str(round(pi, i)) for i in range(1, 6)] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 284 | ['3.1', '3.14', '3.142', '3.1416', '3.14159'] |
| 285 | |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 286 | Nested List Comprehensions |
| 287 | -------------------------- |
| 288 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 289 | The initial expression in a list comprehension can be any arbitrary expression, |
| 290 | including another list comprehension. |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 291 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 292 | Consider the following example of a 3x4 matrix implemented as a list of |
| 293 | 3 lists of length 4:: |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 294 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 295 | >>> matrix = [ |
| 296 | ... [1, 2, 3, 4], |
| 297 | ... [5, 6, 7, 8], |
| 298 | ... [9, 10, 11, 12], |
| 299 | ... ] |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 300 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 301 | The following list comprehension will transpose rows and columns:: |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 302 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 303 | >>> [[row[i] for row in matrix] for i in range(4)] |
| 304 | [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]] |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 305 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 306 | As we saw in the previous section, the nested listcomp is evaluated in |
| 307 | the context of the :keyword:`for` that follows it, so this example is |
| 308 | equivalent to:: |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 309 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 310 | >>> transposed = [] |
| 311 | >>> for i in range(4): |
| 312 | ... transposed.append([row[i] for row in matrix]) |
| 313 | ... |
| 314 | >>> transposed |
| 315 | [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]] |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 316 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 317 | which, in turn, is the same as:: |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 318 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 319 | >>> transposed = [] |
| 320 | >>> for i in range(4): |
| 321 | ... # the following 3 lines implement the nested listcomp |
| 322 | ... transposed_row = [] |
| 323 | ... for row in matrix: |
| 324 | ... transposed_row.append(row[i]) |
| 325 | ... transposed.append(transposed_row) |
| 326 | ... |
| 327 | >>> transposed |
| 328 | [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]] |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 329 | |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 330 | In the real world, you should prefer built-in functions to complex flow statements. |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 331 | The :func:`zip` function would do a great job for this use case:: |
| 332 | |
Sandro Tosi | 0a90a82 | 2012-08-12 10:24:50 +0200 | [diff] [blame] | 333 | >>> list(zip(*matrix)) |
Ezio Melotti | 91621e2 | 2011-12-13 15:36:19 +0200 | [diff] [blame] | 334 | [(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)] |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 335 | |
| 336 | See :ref:`tut-unpacking-arguments` for details on the asterisk in this line. |
| 337 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 338 | .. _tut-del: |
| 339 | |
Serhiy Storchaka | 2b57c43 | 2018-12-19 08:09:46 +0200 | [diff] [blame] | 340 | The :keyword:`!del` statement |
| 341 | ============================= |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 342 | |
| 343 | There is a way to remove an item from a list given its index instead of its |
| 344 | value: the :keyword:`del` statement. This differs from the :meth:`pop` method |
Serhiy Storchaka | 2b57c43 | 2018-12-19 08:09:46 +0200 | [diff] [blame] | 345 | which returns a value. The :keyword:`!del` statement can also be used to remove |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 346 | slices from a list or clear the entire list (which we did earlier by assignment |
| 347 | of an empty list to the slice). For example:: |
| 348 | |
| 349 | >>> a = [-1, 1, 66.25, 333, 333, 1234.5] |
| 350 | >>> del a[0] |
| 351 | >>> a |
| 352 | [1, 66.25, 333, 333, 1234.5] |
| 353 | >>> del a[2:4] |
| 354 | >>> a |
| 355 | [1, 66.25, 1234.5] |
| 356 | >>> del a[:] |
| 357 | >>> a |
| 358 | [] |
| 359 | |
| 360 | :keyword:`del` can also be used to delete entire variables:: |
| 361 | |
| 362 | >>> del a |
| 363 | |
| 364 | Referencing the name ``a`` hereafter is an error (at least until another value |
| 365 | is assigned to it). We'll find other uses for :keyword:`del` later. |
| 366 | |
| 367 | |
Georg Brandl | 5d955ed | 2008-09-13 17:18:21 +0000 | [diff] [blame] | 368 | .. _tut-tuples: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 369 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 370 | Tuples and Sequences |
| 371 | ==================== |
| 372 | |
| 373 | We saw that lists and strings have many common properties, such as indexing and |
| 374 | slicing operations. They are two examples of *sequence* data types (see |
| 375 | :ref:`typesseq`). Since Python is an evolving language, other sequence data |
| 376 | types may be added. There is also another standard sequence data type: the |
| 377 | *tuple*. |
| 378 | |
| 379 | A tuple consists of a number of values separated by commas, for instance:: |
| 380 | |
| 381 | >>> t = 12345, 54321, 'hello!' |
| 382 | >>> t[0] |
| 383 | 12345 |
| 384 | >>> t |
| 385 | (12345, 54321, 'hello!') |
| 386 | >>> # Tuples may be nested: |
| 387 | ... u = t, (1, 2, 3, 4, 5) |
| 388 | >>> u |
| 389 | ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5)) |
Ezio Melotti | f90ea1f | 2012-06-17 14:10:59 +0200 | [diff] [blame] | 390 | >>> # Tuples are immutable: |
| 391 | ... t[0] = 88888 |
| 392 | Traceback (most recent call last): |
| 393 | File "<stdin>", line 1, in <module> |
| 394 | TypeError: 'tuple' object does not support item assignment |
| 395 | >>> # but they can contain mutable objects: |
| 396 | ... v = ([1, 2, 3], [3, 2, 1]) |
| 397 | >>> v |
| 398 | ([1, 2, 3], [3, 2, 1]) |
| 399 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 400 | |
| 401 | As you see, on output tuples are always enclosed in parentheses, so that nested |
| 402 | tuples are interpreted correctly; they may be input with or without surrounding |
| 403 | parentheses, although often parentheses are necessary anyway (if the tuple is |
Ezio Melotti | f90ea1f | 2012-06-17 14:10:59 +0200 | [diff] [blame] | 404 | part of a larger expression). It is not possible to assign to the individual |
| 405 | items of a tuple, however it is possible to create tuples which contain mutable |
| 406 | objects, such as lists. |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 407 | |
Ezio Melotti | f90ea1f | 2012-06-17 14:10:59 +0200 | [diff] [blame] | 408 | Though tuples may seem similar to lists, they are often used in different |
| 409 | situations and for different purposes. |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 410 | Tuples are :term:`immutable`, and usually contain a heterogeneous sequence of |
Ezio Melotti | f90ea1f | 2012-06-17 14:10:59 +0200 | [diff] [blame] | 411 | elements that are accessed via unpacking (see later in this section) or indexing |
| 412 | (or even by attribute in the case of :func:`namedtuples <collections.namedtuple>`). |
| 413 | Lists are :term:`mutable`, and their elements are usually homogeneous and are |
| 414 | accessed by iterating over the list. |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 415 | |
| 416 | A special problem is the construction of tuples containing 0 or 1 items: the |
| 417 | syntax has some extra quirks to accommodate these. Empty tuples are constructed |
| 418 | by an empty pair of parentheses; a tuple with one item is constructed by |
| 419 | following a value with a comma (it is not sufficient to enclose a single value |
| 420 | in parentheses). Ugly, but effective. For example:: |
| 421 | |
| 422 | >>> empty = () |
| 423 | >>> singleton = 'hello', # <-- note trailing comma |
| 424 | >>> len(empty) |
| 425 | 0 |
| 426 | >>> len(singleton) |
| 427 | 1 |
| 428 | >>> singleton |
| 429 | ('hello',) |
| 430 | |
| 431 | The statement ``t = 12345, 54321, 'hello!'`` is an example of *tuple packing*: |
| 432 | the values ``12345``, ``54321`` and ``'hello!'`` are packed together in a tuple. |
| 433 | The reverse operation is also possible:: |
| 434 | |
| 435 | >>> x, y, z = t |
| 436 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 437 | This is called, appropriately enough, *sequence unpacking* and works for any |
Georg Brandl | 7ae90dd | 2009-06-08 18:59:09 +0000 | [diff] [blame] | 438 | sequence on the right-hand side. Sequence unpacking requires that there are as |
| 439 | many variables on the left side of the equals sign as there are elements in the |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 440 | sequence. Note that multiple assignment is really just a combination of tuple |
| 441 | packing and sequence unpacking. |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 442 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 443 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 444 | .. _tut-sets: |
| 445 | |
| 446 | Sets |
| 447 | ==== |
| 448 | |
| 449 | Python also includes a data type for *sets*. A set is an unordered collection |
| 450 | with no duplicate elements. Basic uses include membership testing and |
| 451 | eliminating duplicate entries. Set objects also support mathematical operations |
| 452 | like union, intersection, difference, and symmetric difference. |
| 453 | |
Ezio Melotti | 89b03b0 | 2012-11-17 12:06:01 +0200 | [diff] [blame] | 454 | Curly braces or the :func:`set` function can be used to create sets. Note: to |
Georg Brandl | 10e0e30 | 2009-06-08 20:25:55 +0000 | [diff] [blame] | 455 | create an empty set you have to use ``set()``, not ``{}``; the latter creates an |
| 456 | empty dictionary, a data structure that we discuss in the next section. |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 457 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 458 | Here is a brief demonstration:: |
| 459 | |
Raymond Hettinger | afdeca9 | 2010-08-08 01:30:45 +0000 | [diff] [blame] | 460 | >>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} |
| 461 | >>> print(basket) # show that duplicates have been removed |
Georg Brandl | 1790ed2 | 2010-11-10 07:57:10 +0000 | [diff] [blame] | 462 | {'orange', 'banana', 'pear', 'apple'} |
Raymond Hettinger | afdeca9 | 2010-08-08 01:30:45 +0000 | [diff] [blame] | 463 | >>> 'orange' in basket # fast membership testing |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 464 | True |
Raymond Hettinger | afdeca9 | 2010-08-08 01:30:45 +0000 | [diff] [blame] | 465 | >>> 'crabgrass' in basket |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 466 | False |
| 467 | |
| 468 | >>> # Demonstrate set operations on unique letters from two words |
| 469 | ... |
| 470 | >>> a = set('abracadabra') |
| 471 | >>> b = set('alacazam') |
| 472 | >>> a # unique letters in a |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 473 | {'a', 'r', 'b', 'c', 'd'} |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 474 | >>> a - b # letters in a but not in b |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 475 | {'r', 'd', 'b'} |
KatherineMichel | ca81615 | 2017-06-10 14:19:09 -0500 | [diff] [blame] | 476 | >>> a | b # letters in a or b or both |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 477 | {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'} |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 478 | >>> a & b # letters in both a and b |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 479 | {'a', 'c'} |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 480 | >>> a ^ b # letters in a or b but not both |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 481 | {'r', 'd', 'b', 'm', 'z', 'l'} |
| 482 | |
Ezio Melotti | 89b03b0 | 2012-11-17 12:06:01 +0200 | [diff] [blame] | 483 | Similarly to :ref:`list comprehensions <tut-listcomps>`, set comprehensions |
| 484 | are also supported:: |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 485 | |
| 486 | >>> a = {x for x in 'abracadabra' if x not in 'abc'} |
| 487 | >>> a |
| 488 | {'r', 'd'} |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 489 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 490 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 491 | .. _tut-dictionaries: |
| 492 | |
| 493 | Dictionaries |
| 494 | ============ |
| 495 | |
| 496 | Another useful data type built into Python is the *dictionary* (see |
| 497 | :ref:`typesmapping`). Dictionaries are sometimes found in other languages as |
| 498 | "associative memories" or "associative arrays". Unlike sequences, which are |
| 499 | indexed by a range of numbers, dictionaries are indexed by *keys*, which can be |
| 500 | any immutable type; strings and numbers can always be keys. Tuples can be used |
| 501 | as keys if they contain only strings, numbers, or tuples; if a tuple contains |
| 502 | any mutable object either directly or indirectly, it cannot be used as a key. |
| 503 | You can't use lists as keys, since lists can be modified in place using index |
| 504 | assignments, slice assignments, or methods like :meth:`append` and |
| 505 | :meth:`extend`. |
| 506 | |
hui shang | dfbbbf1 | 2018-04-04 12:55:05 +0800 | [diff] [blame] | 507 | It is best to think of a dictionary as a set of *key: value* pairs, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 508 | with the requirement that the keys are unique (within one dictionary). A pair of |
| 509 | braces creates an empty dictionary: ``{}``. Placing a comma-separated list of |
| 510 | key:value pairs within the braces adds initial key:value pairs to the |
| 511 | dictionary; this is also the way dictionaries are written on output. |
| 512 | |
| 513 | The main operations on a dictionary are storing a value with some key and |
| 514 | extracting the value given the key. It is also possible to delete a key:value |
| 515 | pair with ``del``. If you store using a key that is already in use, the old |
| 516 | value associated with that key is forgotten. It is an error to extract a value |
| 517 | using a non-existent key. |
| 518 | |
hui shang | dfbbbf1 | 2018-04-04 12:55:05 +0800 | [diff] [blame] | 519 | Performing ``list(d)`` on a dictionary returns a list of all the keys |
| 520 | used in the dictionary, in insertion order (if you want it sorted, just use |
| 521 | ``sorted(d)`` instead). To check whether a single key is in the |
Georg Brandl | fc11f27 | 2009-06-16 19:22:10 +0000 | [diff] [blame] | 522 | dictionary, use the :keyword:`in` keyword. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 523 | |
| 524 | Here is a small example using a dictionary:: |
| 525 | |
| 526 | >>> tel = {'jack': 4098, 'sape': 4139} |
| 527 | >>> tel['guido'] = 4127 |
| 528 | >>> tel |
hui shang | dfbbbf1 | 2018-04-04 12:55:05 +0800 | [diff] [blame] | 529 | {'jack': 4098, 'sape': 4139, 'guido': 4127} |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 530 | >>> tel['jack'] |
| 531 | 4098 |
| 532 | >>> del tel['sape'] |
| 533 | >>> tel['irv'] = 4127 |
| 534 | >>> tel |
hui shang | dfbbbf1 | 2018-04-04 12:55:05 +0800 | [diff] [blame] | 535 | {'jack': 4098, 'guido': 4127, 'irv': 4127} |
| 536 | >>> list(tel) |
| 537 | ['jack', 'guido', 'irv'] |
| 538 | >>> sorted(tel) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 539 | ['guido', 'irv', 'jack'] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 540 | >>> 'guido' in tel |
| 541 | True |
Neal Norwitz | e0906d1 | 2007-08-31 03:46:28 +0000 | [diff] [blame] | 542 | >>> 'jack' not in tel |
| 543 | False |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 544 | |
Georg Brandl | fc11f27 | 2009-06-16 19:22:10 +0000 | [diff] [blame] | 545 | The :func:`dict` constructor builds dictionaries directly from sequences of |
Raymond Hettinger | 8699aea | 2009-06-16 20:49:30 +0000 | [diff] [blame] | 546 | key-value pairs:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 547 | |
| 548 | >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]) |
hui shang | dfbbbf1 | 2018-04-04 12:55:05 +0800 | [diff] [blame] | 549 | {'sape': 4139, 'guido': 4127, 'jack': 4098} |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 550 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 551 | In addition, dict comprehensions can be used to create dictionaries from |
| 552 | arbitrary key and value expressions:: |
| 553 | |
| 554 | >>> {x: x**2 for x in (2, 4, 6)} |
| 555 | {2: 4, 4: 16, 6: 36} |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 556 | |
| 557 | When the keys are simple strings, it is sometimes easier to specify pairs using |
| 558 | keyword arguments:: |
| 559 | |
| 560 | >>> dict(sape=4139, guido=4127, jack=4098) |
hui shang | dfbbbf1 | 2018-04-04 12:55:05 +0800 | [diff] [blame] | 561 | {'sape': 4139, 'guido': 4127, 'jack': 4098} |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 562 | |
| 563 | |
| 564 | .. _tut-loopidioms: |
| 565 | |
| 566 | Looping Techniques |
| 567 | ================== |
| 568 | |
| 569 | When looping through dictionaries, the key and corresponding value can be |
Neal Norwitz | e0906d1 | 2007-08-31 03:46:28 +0000 | [diff] [blame] | 570 | retrieved at the same time using the :meth:`items` method. :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 571 | |
| 572 | >>> knights = {'gallahad': 'the pure', 'robin': 'the brave'} |
Neal Norwitz | e0906d1 | 2007-08-31 03:46:28 +0000 | [diff] [blame] | 573 | >>> for k, v in knights.items(): |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 574 | ... print(k, v) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 575 | ... |
| 576 | gallahad the pure |
| 577 | robin the brave |
| 578 | |
| 579 | When looping through a sequence, the position index and corresponding value can |
| 580 | be retrieved at the same time using the :func:`enumerate` function. :: |
| 581 | |
| 582 | >>> for i, v in enumerate(['tic', 'tac', 'toe']): |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 583 | ... print(i, v) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 584 | ... |
| 585 | 0 tic |
| 586 | 1 tac |
| 587 | 2 toe |
| 588 | |
| 589 | To loop over two or more sequences at the same time, the entries can be paired |
| 590 | with the :func:`zip` function. :: |
| 591 | |
| 592 | >>> questions = ['name', 'quest', 'favorite color'] |
| 593 | >>> answers = ['lancelot', 'the holy grail', 'blue'] |
| 594 | >>> for q, a in zip(questions, answers): |
Benjamin Peterson | e6f0063 | 2008-05-26 01:03:56 +0000 | [diff] [blame] | 595 | ... print('What is your {0}? It is {1}.'.format(q, a)) |
Georg Brandl | 06788c9 | 2009-01-03 21:31:47 +0000 | [diff] [blame] | 596 | ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 597 | What is your name? It is lancelot. |
| 598 | What is your quest? It is the holy grail. |
| 599 | What is your favorite color? It is blue. |
| 600 | |
| 601 | To loop over a sequence in reverse, first specify the sequence in a forward |
| 602 | direction and then call the :func:`reversed` function. :: |
| 603 | |
Georg Brandl | e4ac750 | 2007-09-03 07:10:24 +0000 | [diff] [blame] | 604 | >>> for i in reversed(range(1, 10, 2)): |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 605 | ... print(i) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 606 | ... |
| 607 | 9 |
| 608 | 7 |
| 609 | 5 |
| 610 | 3 |
| 611 | 1 |
| 612 | |
| 613 | To loop over a sequence in sorted order, use the :func:`sorted` function which |
| 614 | returns a new sorted list while leaving the source unaltered. :: |
| 615 | |
| 616 | >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] |
Rahul Kumaresan | eefd4e0 | 2020-05-18 07:02:34 +0530 | [diff] [blame] | 617 | >>> for i in sorted(basket): |
| 618 | ... print(i) |
| 619 | ... |
| 620 | apple |
| 621 | apple |
| 622 | banana |
| 623 | orange |
| 624 | orange |
| 625 | pear |
| 626 | |
| 627 | Using :func:`set` on a sequence eliminates duplicate elements. The use of |
| 628 | :func:`sorted` in combination with :func:`set` over a sequence is an idiomatic |
| 629 | way to loop over unique elements of the sequence in sorted order. :: |
| 630 | |
| 631 | >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 632 | >>> for f in sorted(set(basket)): |
Guido van Rossum | 0616b79 | 2007-08-31 03:25:11 +0000 | [diff] [blame] | 633 | ... print(f) |
Georg Brandl | 06788c9 | 2009-01-03 21:31:47 +0000 | [diff] [blame] | 634 | ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 635 | apple |
| 636 | banana |
| 637 | orange |
| 638 | pear |
| 639 | |
Raymond Hettinger | 502bf51 | 2015-09-01 02:33:02 -0700 | [diff] [blame] | 640 | It is sometimes tempting to change a list while you are looping over it; |
| 641 | however, it is often simpler and safer to create a new list instead. :: |
Chris Jerdonek | 4fab8f0 | 2012-10-15 19:44:47 -0700 | [diff] [blame] | 642 | |
Raymond Hettinger | 502bf51 | 2015-09-01 02:33:02 -0700 | [diff] [blame] | 643 | >>> import math |
| 644 | >>> raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8] |
| 645 | >>> filtered_data = [] |
| 646 | >>> for value in raw_data: |
| 647 | ... if not math.isnan(value): |
| 648 | ... filtered_data.append(value) |
Chris Jerdonek | 4fab8f0 | 2012-10-15 19:44:47 -0700 | [diff] [blame] | 649 | ... |
Raymond Hettinger | 502bf51 | 2015-09-01 02:33:02 -0700 | [diff] [blame] | 650 | >>> filtered_data |
| 651 | [56.2, 51.7, 55.3, 52.5, 47.8] |
Chris Jerdonek | 4fab8f0 | 2012-10-15 19:44:47 -0700 | [diff] [blame] | 652 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 653 | |
| 654 | .. _tut-conditions: |
| 655 | |
| 656 | More on Conditions |
| 657 | ================== |
| 658 | |
| 659 | The conditions used in ``while`` and ``if`` statements can contain any |
| 660 | operators, not just comparisons. |
| 661 | |
| 662 | The comparison operators ``in`` and ``not in`` check whether a value occurs |
| 663 | (does not occur) in a sequence. The operators ``is`` and ``is not`` compare |
| 664 | whether two objects are really the same object; this only matters for mutable |
| 665 | objects like lists. All comparison operators have the same priority, which is |
| 666 | lower than that of all numerical operators. |
| 667 | |
| 668 | Comparisons can be chained. For example, ``a < b == c`` tests whether ``a`` is |
| 669 | less than ``b`` and moreover ``b`` equals ``c``. |
| 670 | |
| 671 | Comparisons may be combined using the Boolean operators ``and`` and ``or``, and |
| 672 | the outcome of a comparison (or of any other Boolean expression) may be negated |
| 673 | with ``not``. These have lower priorities than comparison operators; between |
| 674 | them, ``not`` has the highest priority and ``or`` the lowest, so that ``A and |
| 675 | not B or C`` is equivalent to ``(A and (not B)) or C``. As always, parentheses |
| 676 | can be used to express the desired composition. |
| 677 | |
| 678 | The Boolean operators ``and`` and ``or`` are so-called *short-circuit* |
| 679 | operators: their arguments are evaluated from left to right, and evaluation |
| 680 | stops as soon as the outcome is determined. For example, if ``A`` and ``C`` are |
| 681 | true but ``B`` is false, ``A and B and C`` does not evaluate the expression |
| 682 | ``C``. When used as a general value and not as a Boolean, the return value of a |
| 683 | short-circuit operator is the last evaluated argument. |
| 684 | |
| 685 | It is possible to assign the result of a comparison or other Boolean expression |
| 686 | to a variable. For example, :: |
| 687 | |
| 688 | >>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance' |
| 689 | >>> non_null = string1 or string2 or string3 |
| 690 | >>> non_null |
| 691 | 'Trondheim' |
| 692 | |
Ammar Askar | cb2cf06 | 2019-10-25 18:20:05 -0400 | [diff] [blame] | 693 | Note that in Python, unlike C, assignment inside expressions must be done |
Adorilson Bezerra | 5807efd | 2020-02-03 14:11:19 -0300 | [diff] [blame] | 694 | explicitly with the |
| 695 | :ref:`walrus operator <why-can-t-i-use-an-assignment-in-an-expression>` ``:=``. |
| 696 | This avoids a common class of problems encountered in C programs: typing ``=`` |
| 697 | in an expression when ``==`` was intended. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 698 | |
| 699 | |
| 700 | .. _tut-comparing: |
| 701 | |
| 702 | Comparing Sequences and Other Types |
| 703 | =================================== |
Emmanuel Arias | b00479d | 2019-04-02 01:52:42 -0300 | [diff] [blame] | 704 | Sequence objects typically may be compared to other objects with the same sequence |
| 705 | type. The comparison uses *lexicographical* ordering: first the first two |
| 706 | items are compared, and if they differ this determines the outcome of the |
| 707 | comparison; if they are equal, the next two items are compared, and so on, until |
| 708 | either sequence is exhausted. If two items to be compared are themselves |
| 709 | sequences of the same type, the lexicographical comparison is carried out |
| 710 | recursively. If all items of two sequences compare equal, the sequences are |
| 711 | considered equal. If one sequence is an initial sub-sequence of the other, the |
| 712 | shorter sequence is the smaller (lesser) one. Lexicographical ordering for |
| 713 | strings uses the Unicode code point number to order individual characters. |
| 714 | Some examples of comparisons between sequences of the same type:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 715 | |
| 716 | (1, 2, 3) < (1, 2, 4) |
| 717 | [1, 2, 3] < [1, 2, 4] |
| 718 | 'ABC' < 'C' < 'Pascal' < 'Python' |
| 719 | (1, 2, 3, 4) < (1, 2, 4) |
| 720 | (1, 2) < (1, 2, -1) |
| 721 | (1, 2, 3) == (1.0, 2.0, 3.0) |
| 722 | (1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4) |
| 723 | |
Georg Brandl | 9f2c39a | 2007-10-08 14:08:36 +0000 | [diff] [blame] | 724 | Note that comparing objects of different types with ``<`` or ``>`` is legal |
| 725 | provided that the objects have appropriate comparison methods. For example, |
| 726 | mixed numeric types are compared according to their numeric value, so 0 equals |
| 727 | 0.0, etc. Otherwise, rather than providing an arbitrary ordering, the |
| 728 | interpreter will raise a :exc:`TypeError` exception. |
Georg Brandl | fc11f27 | 2009-06-16 19:22:10 +0000 | [diff] [blame] | 729 | |
| 730 | |
| 731 | .. rubric:: Footnotes |
| 732 | |
Georg Brandl | 388349a | 2011-10-08 18:32:40 +0200 | [diff] [blame] | 733 | .. [1] Other languages may return the mutated object, which allows method |
| 734 | chaining, such as ``d->insert("a")->remove("b")->sort();``. |