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