blob: 6140ece046b9754bc752e673c7f36799afd72959 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. _tut-structures:
2
3***************
4Data Structures
5***************
6
7This chapter describes some things you've learned about already in more detail,
8and adds some new things as well.
9
Georg Brandl116aa622007-08-15 14:28:22 +000010.. _tut-morelists:
11
12More on Lists
13=============
14
15The list data type has some more methods. Here are all of the methods of list
16objects:
17
18
19.. method:: list.append(x)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000020 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000021
Georg Brandl388349a2011-10-08 18:32:40 +020022 Add an item to the end of the list. Equivalent to ``a[len(a):] = [x]``.
Georg Brandl116aa622007-08-15 14:28:22 +000023
24
Mariattab0023282017-02-25 22:34:51 -080025.. method:: list.extend(iterable)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000026 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000027
Mariattab0023282017-02-25 22:34:51 -080028 Extend the list by appending all the items from the iterable. Equivalent to
29 ``a[len(a):] = iterable``.
Georg Brandl116aa622007-08-15 14:28:22 +000030
31
32.. method:: list.insert(i, x)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000033 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000034
35 Insert an item at a given position. The first argument is the index of the
36 element before which to insert, so ``a.insert(0, x)`` inserts at the front of
37 the list, and ``a.insert(len(a), x)`` is equivalent to ``a.append(x)``.
38
39
40.. method:: list.remove(x)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000041 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000042
Georg Brandl388349a2011-10-08 18:32:40 +020043 Remove the first item from the list whose value is *x*. It is an error if
44 there is no such item.
Georg Brandl116aa622007-08-15 14:28:22 +000045
46
47.. method:: list.pop([i])
Christian Heimes4fbc72b2008-03-22 00:47:35 +000048 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000049
50 Remove the item at the given position in the list, and return it. If no index
51 is specified, ``a.pop()`` removes and returns the last item in the list. (The
52 square brackets around the *i* in the method signature denote that the parameter
53 is optional, not that you should type square brackets at that position. You
54 will see this notation frequently in the Python Library Reference.)
55
56
Georg Brandla12b6822013-10-06 13:01:19 +020057.. method:: list.clear()
58 :noindex:
59
60 Remove all items from the list. Equivalent to ``del a[:]``.
61
62
Raymond Hettinger5bd5b9d2016-11-21 15:12:54 -080063.. method:: list.index(x[, start[, end]])
Christian Heimes4fbc72b2008-03-22 00:47:35 +000064 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000065
Raymond Hettinger5bd5b9d2016-11-21 15:12:54 -080066 Return zero-based index in the list of the first item whose value is *x*.
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
Mariattab0023282017-02-25 22:34:51 -080071 the list. The returned index is computed relative to the beginning of the full
Raymond Hettinger5bd5b9d2016-11-21 15:12:54 -080072 sequence rather than the *start* argument.
Georg Brandl116aa622007-08-15 14:28:22 +000073
74
75.. method:: list.count(x)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000076 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000077
78 Return the number of times *x* appears in the list.
79
80
Raymond Hettinger07e04852014-05-26 18:44:04 -070081.. method:: list.sort(key=None, reverse=False)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000082 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000083
Raymond Hettinger07e04852014-05-26 18:44:04 -070084 Sort the items of the list in place (the arguments can be used for sort
85 customization, see :func:`sorted` for their explanation).
Georg Brandl116aa622007-08-15 14:28:22 +000086
87
88.. method:: list.reverse()
Christian Heimes4fbc72b2008-03-22 00:47:35 +000089 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000090
Georg Brandl388349a2011-10-08 18:32:40 +020091 Reverse the elements of the list in place.
92
Georg Brandl116aa622007-08-15 14:28:22 +000093
Georg Brandla12b6822013-10-06 13:01:19 +020094.. method:: list.copy()
95 :noindex:
96
97 Return a shallow copy of the list. Equivalent to ``a[:]``.
98
99
Georg Brandl116aa622007-08-15 14:28:22 +0000100An example that uses most of the list methods::
101
Raymond Hettinger8c5e1902016-11-21 16:29:50 -0800102 >>> 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 Brandl116aa622007-08-15 14:28:22 +0000122
Georg Brandl388349a2011-10-08 18:32:40 +0200123You might have noticed that methods like ``insert``, ``remove`` or ``sort`` that
Terry Jan Reedye17de092014-05-23 00:34:12 -0400124only 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
126Python.
Georg Brandl388349a2011-10-08 18:32:40 +0200127
Georg Brandl116aa622007-08-15 14:28:22 +0000128
129.. _tut-lists-as-stacks:
130
131Using Lists as Stacks
132---------------------
133
134.. sectionauthor:: Ka-Ping Yee <ping@lfw.org>
135
136
137The list methods make it very easy to use a list as a stack, where the last
138element added is the first element retrieved ("last-in, first-out"). To add an
139item to the top of the stack, use :meth:`append`. To retrieve an item from the
140top of the stack, use :meth:`pop` without an explicit index. For example::
141
142 >>> stack = [3, 4, 5]
143 >>> stack.append(6)
144 >>> stack.append(7)
145 >>> stack
146 [3, 4, 5, 6, 7]
147 >>> stack.pop()
148 7
149 >>> stack
150 [3, 4, 5, 6]
151 >>> stack.pop()
152 6
153 >>> stack.pop()
154 5
155 >>> stack
156 [3, 4]
157
158
159.. _tut-lists-as-queues:
160
161Using Lists as Queues
162---------------------
163
164.. sectionauthor:: Ka-Ping Yee <ping@lfw.org>
165
Ezio Melotti8f8db142010-03-31 07:45:32 +0000166It is also possible to use a list as a queue, where the first element added is
167the first element retrieved ("first-in, first-out"); however, lists are not
168efficient for this purpose. While appends and pops from the end of list are
169fast, doing inserts or pops from the beginning of a list is slow (because all
170of the other elements have to be shifted by one).
Georg Brandl116aa622007-08-15 14:28:22 +0000171
Ezio Melotti8f8db142010-03-31 07:45:32 +0000172To implement a queue, use :class:`collections.deque` which was designed to
173have fast appends and pops from both ends. For example::
Georg Brandl116aa622007-08-15 14:28:22 +0000174
Ezio Melotti8f8db142010-03-31 07:45:32 +0000175 >>> from collections import deque
176 >>> queue = deque(["Eric", "John", "Michael"])
Georg Brandl116aa622007-08-15 14:28:22 +0000177 >>> queue.append("Terry") # Terry arrives
178 >>> queue.append("Graham") # Graham arrives
Ezio Melotti8f8db142010-03-31 07:45:32 +0000179 >>> queue.popleft() # The first to arrive now leaves
Georg Brandl116aa622007-08-15 14:28:22 +0000180 'Eric'
Ezio Melotti8f8db142010-03-31 07:45:32 +0000181 >>> queue.popleft() # The second to arrive now leaves
Georg Brandl116aa622007-08-15 14:28:22 +0000182 'John'
Ezio Melotti8f8db142010-03-31 07:45:32 +0000183 >>> queue # Remaining queue in order of arrival
184 deque(['Michael', 'Terry', 'Graham'])
Georg Brandl718ce2c2010-03-21 09:51:44 +0000185
Georg Brandl116aa622007-08-15 14:28:22 +0000186
Georg Brandlfc11f272009-06-16 19:22:10 +0000187.. _tut-listcomps:
188
Georg Brandl116aa622007-08-15 14:28:22 +0000189List Comprehensions
190-------------------
191
Ezio Melotti91621e22011-12-13 15:36:19 +0200192List comprehensions provide a concise way to create lists.
193Common applications are to make new lists where each element is the result of
194some operations applied to each member of another sequence or iterable, or to
195create a subsequence of those elements that satisfy a certain condition.
196
197For example, assume we want to create a list of squares, like::
198
199 >>> squares = []
200 >>> for x in range(10):
201 ... squares.append(x**2)
202 ...
203 >>> squares
204 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
205
R David Murray6bd68602014-09-30 21:25:38 -0400206Note that this creates (or overwrites) a variable named ``x`` that still exists
207after the loop completes. We can calculate the list of squares without any
208side effects using::
209
210 squares = list(map(lambda x: x**2, range(10)))
211
212or, equivalently::
Ezio Melotti91621e22011-12-13 15:36:19 +0200213
214 squares = [x**2 for x in range(10)]
215
R David Murray6bd68602014-09-30 21:25:38 -0400216which is more concise and readable.
Guido van Rossum0616b792007-08-31 03:25:11 +0000217
Georg Brandl7ae90dd2009-06-08 18:59:09 +0000218A list comprehension consists of brackets containing an expression followed
219by a :keyword:`for` clause, then zero or more :keyword:`for` or :keyword:`if`
Ezio Melotti91621e22011-12-13 15:36:19 +0200220clauses. The result will be a new list resulting from evaluating the expression
221in the context of the :keyword:`for` and :keyword:`if` clauses which follow it.
222For example, this listcomp combines the elements of two lists if they are not
223equal::
Guido van Rossum0616b792007-08-31 03:25:11 +0000224
Ezio Melotti91621e22011-12-13 15:36:19 +0200225 >>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
226 [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
Guido van Rossum0616b792007-08-31 03:25:11 +0000227
Ezio Melotti91621e22011-12-13 15:36:19 +0200228and it's equivalent to::
Guido van Rossum0616b792007-08-31 03:25:11 +0000229
Ezio Melotti91621e22011-12-13 15:36:19 +0200230 >>> combs = []
231 >>> for x in [1,2,3]:
232 ... for y in [3,1,4]:
233 ... if x != y:
234 ... combs.append((x, y))
235 ...
236 >>> combs
237 [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
Guido van Rossum0616b792007-08-31 03:25:11 +0000238
Ezio Melotti91621e22011-12-13 15:36:19 +0200239Note how the order of the :keyword:`for` and :keyword:`if` statements is the
240same in both these snippets.
Guido van Rossum0616b792007-08-31 03:25:11 +0000241
Ezio Melotti91621e22011-12-13 15:36:19 +0200242If the expression is a tuple (e.g. the ``(x, y)`` in the previous example),
243it must be parenthesized. ::
Georg Brandl116aa622007-08-15 14:28:22 +0000244
Ezio Melotti91621e22011-12-13 15:36:19 +0200245 >>> vec = [-4, -2, 0, 2, 4]
246 >>> # create a new list with the values doubled
247 >>> [x*2 for x in vec]
248 [-8, -4, 0, 4, 8]
249 >>> # filter the list to exclude negative numbers
250 >>> [x for x in vec if x >= 0]
251 [0, 2, 4]
252 >>> # apply a function to all the elements
253 >>> [abs(x) for x in vec]
254 [4, 2, 0, 2, 4]
255 >>> # call a method on each element
Georg Brandl116aa622007-08-15 14:28:22 +0000256 >>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
257 >>> [weapon.strip() for weapon in freshfruit]
258 ['banana', 'loganberry', 'passion fruit']
Ezio Melotti91621e22011-12-13 15:36:19 +0200259 >>> # create a list of 2-tuples like (number, square)
260 >>> [(x, x**2) for x in range(6)]
261 [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
262 >>> # the tuple must be parenthesized, otherwise an error is raised
263 >>> [x, x**2 for x in range(6)]
Georg Brandl116aa622007-08-15 14:28:22 +0000264 File "<stdin>", line 1, in ?
Ezio Melotti91621e22011-12-13 15:36:19 +0200265 [x, x**2 for x in range(6)]
Georg Brandl116aa622007-08-15 14:28:22 +0000266 ^
267 SyntaxError: invalid syntax
Ezio Melotti91621e22011-12-13 15:36:19 +0200268 >>> # flatten a list using a listcomp with two 'for'
269 >>> vec = [[1,2,3], [4,5,6], [7,8,9]]
270 >>> [num for elem in vec for num in elem]
271 [1, 2, 3, 4, 5, 6, 7, 8, 9]
Guido van Rossum0616b792007-08-31 03:25:11 +0000272
Ezio Melotti91621e22011-12-13 15:36:19 +0200273List comprehensions can contain complex expressions and nested functions::
Guido van Rossum0616b792007-08-31 03:25:11 +0000274
Ezio Melotti91621e22011-12-13 15:36:19 +0200275 >>> from math import pi
276 >>> [str(round(pi, i)) for i in range(1, 6)]
Georg Brandl116aa622007-08-15 14:28:22 +0000277 ['3.1', '3.14', '3.142', '3.1416', '3.14159']
278
Christian Heimes0449f632007-12-15 01:27:15 +0000279Nested List Comprehensions
280--------------------------
281
Ezio Melotti91621e22011-12-13 15:36:19 +0200282The initial expression in a list comprehension can be any arbitrary expression,
283including another list comprehension.
Christian Heimes0449f632007-12-15 01:27:15 +0000284
Ezio Melotti91621e22011-12-13 15:36:19 +0200285Consider the following example of a 3x4 matrix implemented as a list of
2863 lists of length 4::
Christian Heimes0449f632007-12-15 01:27:15 +0000287
Ezio Melotti91621e22011-12-13 15:36:19 +0200288 >>> matrix = [
289 ... [1, 2, 3, 4],
290 ... [5, 6, 7, 8],
291 ... [9, 10, 11, 12],
292 ... ]
Christian Heimes0449f632007-12-15 01:27:15 +0000293
Ezio Melotti91621e22011-12-13 15:36:19 +0200294The following list comprehension will transpose rows and columns::
Christian Heimes0449f632007-12-15 01:27:15 +0000295
Ezio Melotti91621e22011-12-13 15:36:19 +0200296 >>> [[row[i] for row in matrix] for i in range(4)]
297 [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
Christian Heimes0449f632007-12-15 01:27:15 +0000298
Ezio Melotti91621e22011-12-13 15:36:19 +0200299As we saw in the previous section, the nested listcomp is evaluated in
300the context of the :keyword:`for` that follows it, so this example is
301equivalent to::
Christian Heimes0449f632007-12-15 01:27:15 +0000302
Ezio Melotti91621e22011-12-13 15:36:19 +0200303 >>> transposed = []
304 >>> for i in range(4):
305 ... transposed.append([row[i] for row in matrix])
306 ...
307 >>> transposed
308 [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
Christian Heimes0449f632007-12-15 01:27:15 +0000309
Ezio Melotti91621e22011-12-13 15:36:19 +0200310which, in turn, is the same as::
Christian Heimes0449f632007-12-15 01:27:15 +0000311
Ezio Melotti91621e22011-12-13 15:36:19 +0200312 >>> transposed = []
313 >>> for i in range(4):
314 ... # the following 3 lines implement the nested listcomp
315 ... transposed_row = []
316 ... for row in matrix:
317 ... transposed_row.append(row[i])
318 ... transposed.append(transposed_row)
319 ...
320 >>> transposed
321 [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
Christian Heimes0449f632007-12-15 01:27:15 +0000322
Ezio Melotti91621e22011-12-13 15:36:19 +0200323In the real world, you should prefer built-in functions to complex flow statements.
Christian Heimes0449f632007-12-15 01:27:15 +0000324The :func:`zip` function would do a great job for this use case::
325
Sandro Tosi0a90a822012-08-12 10:24:50 +0200326 >>> list(zip(*matrix))
Ezio Melotti91621e22011-12-13 15:36:19 +0200327 [(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
Christian Heimes0449f632007-12-15 01:27:15 +0000328
329See :ref:`tut-unpacking-arguments` for details on the asterisk in this line.
330
Georg Brandl116aa622007-08-15 14:28:22 +0000331.. _tut-del:
332
333The :keyword:`del` statement
334============================
335
336There is a way to remove an item from a list given its index instead of its
337value: the :keyword:`del` statement. This differs from the :meth:`pop` method
338which returns a value. The :keyword:`del` statement can also be used to remove
339slices from a list or clear the entire list (which we did earlier by assignment
340of an empty list to the slice). For example::
341
342 >>> a = [-1, 1, 66.25, 333, 333, 1234.5]
343 >>> del a[0]
344 >>> a
345 [1, 66.25, 333, 333, 1234.5]
346 >>> del a[2:4]
347 >>> a
348 [1, 66.25, 1234.5]
349 >>> del a[:]
350 >>> a
351 []
352
353:keyword:`del` can also be used to delete entire variables::
354
355 >>> del a
356
357Referencing the name ``a`` hereafter is an error (at least until another value
358is assigned to it). We'll find other uses for :keyword:`del` later.
359
360
Georg Brandl5d955ed2008-09-13 17:18:21 +0000361.. _tut-tuples:
Georg Brandl116aa622007-08-15 14:28:22 +0000362
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000363Tuples and Sequences
364====================
365
366We saw that lists and strings have many common properties, such as indexing and
367slicing operations. They are two examples of *sequence* data types (see
368:ref:`typesseq`). Since Python is an evolving language, other sequence data
369types may be added. There is also another standard sequence data type: the
370*tuple*.
371
372A tuple consists of a number of values separated by commas, for instance::
373
374 >>> t = 12345, 54321, 'hello!'
375 >>> t[0]
376 12345
377 >>> t
378 (12345, 54321, 'hello!')
379 >>> # Tuples may be nested:
380 ... u = t, (1, 2, 3, 4, 5)
381 >>> u
382 ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
Ezio Melottif90ea1f2012-06-17 14:10:59 +0200383 >>> # Tuples are immutable:
384 ... t[0] = 88888
385 Traceback (most recent call last):
386 File "<stdin>", line 1, in <module>
387 TypeError: 'tuple' object does not support item assignment
388 >>> # but they can contain mutable objects:
389 ... v = ([1, 2, 3], [3, 2, 1])
390 >>> v
391 ([1, 2, 3], [3, 2, 1])
392
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000393
394As you see, on output tuples are always enclosed in parentheses, so that nested
395tuples are interpreted correctly; they may be input with or without surrounding
396parentheses, although often parentheses are necessary anyway (if the tuple is
Ezio Melottif90ea1f2012-06-17 14:10:59 +0200397part of a larger expression). It is not possible to assign to the individual
398items of a tuple, however it is possible to create tuples which contain mutable
399objects, such as lists.
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000400
Ezio Melottif90ea1f2012-06-17 14:10:59 +0200401Though tuples may seem similar to lists, they are often used in different
402situations and for different purposes.
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300403Tuples are :term:`immutable`, and usually contain a heterogeneous sequence of
Ezio Melottif90ea1f2012-06-17 14:10:59 +0200404elements that are accessed via unpacking (see later in this section) or indexing
405(or even by attribute in the case of :func:`namedtuples <collections.namedtuple>`).
406Lists are :term:`mutable`, and their elements are usually homogeneous and are
407accessed by iterating over the list.
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000408
409A special problem is the construction of tuples containing 0 or 1 items: the
410syntax has some extra quirks to accommodate these. Empty tuples are constructed
411by an empty pair of parentheses; a tuple with one item is constructed by
412following a value with a comma (it is not sufficient to enclose a single value
413in parentheses). Ugly, but effective. For example::
414
415 >>> empty = ()
416 >>> singleton = 'hello', # <-- note trailing comma
417 >>> len(empty)
418 0
419 >>> len(singleton)
420 1
421 >>> singleton
422 ('hello',)
423
424The statement ``t = 12345, 54321, 'hello!'`` is an example of *tuple packing*:
425the values ``12345``, ``54321`` and ``'hello!'`` are packed together in a tuple.
426The reverse operation is also possible::
427
428 >>> x, y, z = t
429
Benjamin Petersond23f8222009-04-05 19:13:16 +0000430This is called, appropriately enough, *sequence unpacking* and works for any
Georg Brandl7ae90dd2009-06-08 18:59:09 +0000431sequence on the right-hand side. Sequence unpacking requires that there are as
432many variables on the left side of the equals sign as there are elements in the
Benjamin Petersond23f8222009-04-05 19:13:16 +0000433sequence. Note that multiple assignment is really just a combination of tuple
434packing and sequence unpacking.
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000435
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000436
Georg Brandl116aa622007-08-15 14:28:22 +0000437.. _tut-sets:
438
439Sets
440====
441
442Python also includes a data type for *sets*. A set is an unordered collection
443with no duplicate elements. Basic uses include membership testing and
444eliminating duplicate entries. Set objects also support mathematical operations
445like union, intersection, difference, and symmetric difference.
446
Ezio Melotti89b03b02012-11-17 12:06:01 +0200447Curly braces or the :func:`set` function can be used to create sets. Note: to
Georg Brandl10e0e302009-06-08 20:25:55 +0000448create an empty set you have to use ``set()``, not ``{}``; the latter creates an
449empty dictionary, a data structure that we discuss in the next section.
Guido van Rossum0616b792007-08-31 03:25:11 +0000450
Georg Brandl116aa622007-08-15 14:28:22 +0000451Here is a brief demonstration::
452
Raymond Hettingerafdeca92010-08-08 01:30:45 +0000453 >>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
454 >>> print(basket) # show that duplicates have been removed
Georg Brandl1790ed22010-11-10 07:57:10 +0000455 {'orange', 'banana', 'pear', 'apple'}
Raymond Hettingerafdeca92010-08-08 01:30:45 +0000456 >>> 'orange' in basket # fast membership testing
Georg Brandl116aa622007-08-15 14:28:22 +0000457 True
Raymond Hettingerafdeca92010-08-08 01:30:45 +0000458 >>> 'crabgrass' in basket
Georg Brandl116aa622007-08-15 14:28:22 +0000459 False
460
461 >>> # Demonstrate set operations on unique letters from two words
462 ...
463 >>> a = set('abracadabra')
464 >>> b = set('alacazam')
465 >>> a # unique letters in a
Guido van Rossum0616b792007-08-31 03:25:11 +0000466 {'a', 'r', 'b', 'c', 'd'}
Georg Brandl116aa622007-08-15 14:28:22 +0000467 >>> a - b # letters in a but not in b
Guido van Rossum0616b792007-08-31 03:25:11 +0000468 {'r', 'd', 'b'}
Georg Brandl116aa622007-08-15 14:28:22 +0000469 >>> a | b # letters in either a or b
Guido van Rossum0616b792007-08-31 03:25:11 +0000470 {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
Georg Brandl116aa622007-08-15 14:28:22 +0000471 >>> a & b # letters in both a and b
Guido van Rossum0616b792007-08-31 03:25:11 +0000472 {'a', 'c'}
Georg Brandl116aa622007-08-15 14:28:22 +0000473 >>> a ^ b # letters in a or b but not both
Guido van Rossum0616b792007-08-31 03:25:11 +0000474 {'r', 'd', 'b', 'm', 'z', 'l'}
475
Ezio Melotti89b03b02012-11-17 12:06:01 +0200476Similarly to :ref:`list comprehensions <tut-listcomps>`, set comprehensions
477are also supported::
Georg Brandlf6945182008-02-01 11:56:49 +0000478
479 >>> a = {x for x in 'abracadabra' if x not in 'abc'}
480 >>> a
481 {'r', 'd'}
Guido van Rossum0616b792007-08-31 03:25:11 +0000482
Georg Brandl116aa622007-08-15 14:28:22 +0000483
Georg Brandl116aa622007-08-15 14:28:22 +0000484.. _tut-dictionaries:
485
486Dictionaries
487============
488
489Another useful data type built into Python is the *dictionary* (see
490:ref:`typesmapping`). Dictionaries are sometimes found in other languages as
491"associative memories" or "associative arrays". Unlike sequences, which are
492indexed by a range of numbers, dictionaries are indexed by *keys*, which can be
493any immutable type; strings and numbers can always be keys. Tuples can be used
494as keys if they contain only strings, numbers, or tuples; if a tuple contains
495any mutable object either directly or indirectly, it cannot be used as a key.
496You can't use lists as keys, since lists can be modified in place using index
497assignments, slice assignments, or methods like :meth:`append` and
498:meth:`extend`.
499
500It is best to think of a dictionary as an unordered set of *key: value* pairs,
501with the requirement that the keys are unique (within one dictionary). A pair of
502braces creates an empty dictionary: ``{}``. Placing a comma-separated list of
503key:value pairs within the braces adds initial key:value pairs to the
504dictionary; this is also the way dictionaries are written on output.
505
506The main operations on a dictionary are storing a value with some key and
507extracting the value given the key. It is also possible to delete a key:value
508pair with ``del``. If you store using a key that is already in use, the old
509value associated with that key is forgotten. It is an error to extract a value
510using a non-existent key.
511
Georg Brandlabffe712008-12-15 08:28:37 +0000512Performing ``list(d.keys())`` on a dictionary returns a list of all the keys
Georg Brandlfc11f272009-06-16 19:22:10 +0000513used in the dictionary, in arbitrary order (if you want it sorted, just use
Georg Brandl388349a2011-10-08 18:32:40 +0200514``sorted(d.keys())`` instead). [2]_ To check whether a single key is in the
Georg Brandlfc11f272009-06-16 19:22:10 +0000515dictionary, use the :keyword:`in` keyword.
Georg Brandl116aa622007-08-15 14:28:22 +0000516
517Here is a small example using a dictionary::
518
519 >>> tel = {'jack': 4098, 'sape': 4139}
520 >>> tel['guido'] = 4127
521 >>> tel
522 {'sape': 4139, 'guido': 4127, 'jack': 4098}
523 >>> tel['jack']
524 4098
525 >>> del tel['sape']
526 >>> tel['irv'] = 4127
527 >>> tel
528 {'guido': 4127, 'irv': 4127, 'jack': 4098}
Neal Norwitze0906d12007-08-31 03:46:28 +0000529 >>> list(tel.keys())
Georg Brandlabffe712008-12-15 08:28:37 +0000530 ['irv', 'guido', 'jack']
531 >>> sorted(tel.keys())
Georg Brandl116aa622007-08-15 14:28:22 +0000532 ['guido', 'irv', 'jack']
Georg Brandl116aa622007-08-15 14:28:22 +0000533 >>> 'guido' in tel
534 True
Neal Norwitze0906d12007-08-31 03:46:28 +0000535 >>> 'jack' not in tel
536 False
Georg Brandl116aa622007-08-15 14:28:22 +0000537
Georg Brandlfc11f272009-06-16 19:22:10 +0000538The :func:`dict` constructor builds dictionaries directly from sequences of
Raymond Hettinger8699aea2009-06-16 20:49:30 +0000539key-value pairs::
Georg Brandl116aa622007-08-15 14:28:22 +0000540
541 >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
542 {'sape': 4139, 'jack': 4098, 'guido': 4127}
Georg Brandl116aa622007-08-15 14:28:22 +0000543
Georg Brandlf6945182008-02-01 11:56:49 +0000544In addition, dict comprehensions can be used to create dictionaries from
545arbitrary key and value expressions::
546
547 >>> {x: x**2 for x in (2, 4, 6)}
548 {2: 4, 4: 16, 6: 36}
Georg Brandl116aa622007-08-15 14:28:22 +0000549
550When the keys are simple strings, it is sometimes easier to specify pairs using
551keyword arguments::
552
553 >>> dict(sape=4139, guido=4127, jack=4098)
554 {'sape': 4139, 'jack': 4098, 'guido': 4127}
555
556
557.. _tut-loopidioms:
558
559Looping Techniques
560==================
561
562When looping through dictionaries, the key and corresponding value can be
Neal Norwitze0906d12007-08-31 03:46:28 +0000563retrieved at the same time using the :meth:`items` method. ::
Georg Brandl116aa622007-08-15 14:28:22 +0000564
565 >>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
Neal Norwitze0906d12007-08-31 03:46:28 +0000566 >>> for k, v in knights.items():
Guido van Rossum0616b792007-08-31 03:25:11 +0000567 ... print(k, v)
Georg Brandl116aa622007-08-15 14:28:22 +0000568 ...
569 gallahad the pure
570 robin the brave
571
572When looping through a sequence, the position index and corresponding value can
573be retrieved at the same time using the :func:`enumerate` function. ::
574
575 >>> for i, v in enumerate(['tic', 'tac', 'toe']):
Guido van Rossum0616b792007-08-31 03:25:11 +0000576 ... print(i, v)
Georg Brandl116aa622007-08-15 14:28:22 +0000577 ...
578 0 tic
579 1 tac
580 2 toe
581
582To loop over two or more sequences at the same time, the entries can be paired
583with the :func:`zip` function. ::
584
585 >>> questions = ['name', 'quest', 'favorite color']
586 >>> answers = ['lancelot', 'the holy grail', 'blue']
587 >>> for q, a in zip(questions, answers):
Benjamin Petersone6f00632008-05-26 01:03:56 +0000588 ... print('What is your {0}? It is {1}.'.format(q, a))
Georg Brandl06788c92009-01-03 21:31:47 +0000589 ...
Georg Brandl116aa622007-08-15 14:28:22 +0000590 What is your name? It is lancelot.
591 What is your quest? It is the holy grail.
592 What is your favorite color? It is blue.
593
594To loop over a sequence in reverse, first specify the sequence in a forward
595direction and then call the :func:`reversed` function. ::
596
Georg Brandle4ac7502007-09-03 07:10:24 +0000597 >>> for i in reversed(range(1, 10, 2)):
Guido van Rossum0616b792007-08-31 03:25:11 +0000598 ... print(i)
Georg Brandl116aa622007-08-15 14:28:22 +0000599 ...
600 9
601 7
602 5
603 3
604 1
605
606To loop over a sequence in sorted order, use the :func:`sorted` function which
607returns a new sorted list while leaving the source unaltered. ::
608
609 >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
610 >>> for f in sorted(set(basket)):
Guido van Rossum0616b792007-08-31 03:25:11 +0000611 ... print(f)
Georg Brandl06788c92009-01-03 21:31:47 +0000612 ...
Georg Brandl116aa622007-08-15 14:28:22 +0000613 apple
614 banana
615 orange
616 pear
617
Raymond Hettinger502bf512015-09-01 02:33:02 -0700618It is sometimes tempting to change a list while you are looping over it;
619however, it is often simpler and safer to create a new list instead. ::
Chris Jerdonek4fab8f02012-10-15 19:44:47 -0700620
Raymond Hettinger502bf512015-09-01 02:33:02 -0700621 >>> import math
622 >>> raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]
623 >>> filtered_data = []
624 >>> for value in raw_data:
625 ... if not math.isnan(value):
626 ... filtered_data.append(value)
Chris Jerdonek4fab8f02012-10-15 19:44:47 -0700627 ...
Raymond Hettinger502bf512015-09-01 02:33:02 -0700628 >>> filtered_data
629 [56.2, 51.7, 55.3, 52.5, 47.8]
Chris Jerdonek4fab8f02012-10-15 19:44:47 -0700630
Georg Brandl116aa622007-08-15 14:28:22 +0000631
632.. _tut-conditions:
633
634More on Conditions
635==================
636
637The conditions used in ``while`` and ``if`` statements can contain any
638operators, not just comparisons.
639
640The comparison operators ``in`` and ``not in`` check whether a value occurs
641(does not occur) in a sequence. The operators ``is`` and ``is not`` compare
642whether two objects are really the same object; this only matters for mutable
643objects like lists. All comparison operators have the same priority, which is
644lower than that of all numerical operators.
645
646Comparisons can be chained. For example, ``a < b == c`` tests whether ``a`` is
647less than ``b`` and moreover ``b`` equals ``c``.
648
649Comparisons may be combined using the Boolean operators ``and`` and ``or``, and
650the outcome of a comparison (or of any other Boolean expression) may be negated
651with ``not``. These have lower priorities than comparison operators; between
652them, ``not`` has the highest priority and ``or`` the lowest, so that ``A and
653not B or C`` is equivalent to ``(A and (not B)) or C``. As always, parentheses
654can be used to express the desired composition.
655
656The Boolean operators ``and`` and ``or`` are so-called *short-circuit*
657operators: their arguments are evaluated from left to right, and evaluation
658stops as soon as the outcome is determined. For example, if ``A`` and ``C`` are
659true but ``B`` is false, ``A and B and C`` does not evaluate the expression
660``C``. When used as a general value and not as a Boolean, the return value of a
661short-circuit operator is the last evaluated argument.
662
663It is possible to assign the result of a comparison or other Boolean expression
664to a variable. For example, ::
665
666 >>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
667 >>> non_null = string1 or string2 or string3
668 >>> non_null
669 'Trondheim'
670
671Note that in Python, unlike C, assignment cannot occur inside expressions. C
672programmers may grumble about this, but it avoids a common class of problems
673encountered in C programs: typing ``=`` in an expression when ``==`` was
674intended.
675
676
677.. _tut-comparing:
678
679Comparing Sequences and Other Types
680===================================
681
682Sequence objects may be compared to other objects with the same sequence type.
683The comparison uses *lexicographical* ordering: first the first two items are
684compared, and if they differ this determines the outcome of the comparison; if
685they are equal, the next two items are compared, and so on, until either
686sequence is exhausted. If two items to be compared are themselves sequences of
687the same type, the lexicographical comparison is carried out recursively. If
688all items of two sequences compare equal, the sequences are considered equal.
689If one sequence is an initial sub-sequence of the other, the shorter sequence is
Georg Brandlfc11f272009-06-16 19:22:10 +0000690the smaller (lesser) one. Lexicographical ordering for strings uses the Unicode
Georg Brandl3be472b2015-01-14 08:26:30 +0100691code point number to order individual characters. Some examples of comparisons
Georg Brandlfc11f272009-06-16 19:22:10 +0000692between sequences of the same type::
Georg Brandl116aa622007-08-15 14:28:22 +0000693
694 (1, 2, 3) < (1, 2, 4)
695 [1, 2, 3] < [1, 2, 4]
696 'ABC' < 'C' < 'Pascal' < 'Python'
697 (1, 2, 3, 4) < (1, 2, 4)
698 (1, 2) < (1, 2, -1)
699 (1, 2, 3) == (1.0, 2.0, 3.0)
700 (1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
701
Georg Brandl9f2c39a2007-10-08 14:08:36 +0000702Note that comparing objects of different types with ``<`` or ``>`` is legal
703provided that the objects have appropriate comparison methods. For example,
704mixed numeric types are compared according to their numeric value, so 0 equals
7050.0, etc. Otherwise, rather than providing an arbitrary ordering, the
706interpreter will raise a :exc:`TypeError` exception.
Georg Brandlfc11f272009-06-16 19:22:10 +0000707
708
709.. rubric:: Footnotes
710
Georg Brandl388349a2011-10-08 18:32:40 +0200711.. [1] Other languages may return the mutated object, which allows method
712 chaining, such as ``d->insert("a")->remove("b")->sort();``.
713
714.. [2] Calling ``d.keys()`` will return a :dfn:`dictionary view` object. It
Georg Brandlfc11f272009-06-16 19:22:10 +0000715 supports operations like membership test and iteration, but its contents
716 are not independent of the original dictionary -- it is only a *view*.