blob: ff4c797f66cd63163457a5553cf813e63158c3e3 [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
Jim Fasarakis-Hilliard53c18922017-02-25 23:13:33 +020025.. method:: list.extend(iterable)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000026 :noindex:
Georg Brandl116aa622007-08-15 14:28:22 +000027
Jim Fasarakis-Hilliard53c18922017-02-25 23:13:33 +020028 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
Lysandros Nikolaoubcd1d972018-08-03 04:45:48 +020043 Remove the first item from the list whose value is equal to *x*. It raises a
Stéphane Wirtele483f022018-10-26 12:52:11 +020044 :exc:`ValueError` if 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
Xiang Zhangb2d77172017-03-13 10:09:16 +080066 Return zero-based index in the list of the first item whose value is equal to *x*.
Raymond Hettinger5bd5b9d2016-11-21 15:12:54 -080067 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-Hilliard53c18922017-02-25 23:13:33 +020071 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
Raymond Hettinger41092632019-08-22 09:11:35 -0700128Another thing you might notice is that not all data can be sorted or
129compared. For instance, ``[None, 'hello', 10]`` doesn't sort because
130integers can't be compared to strings and *None* can't be compared to
131other types. Also, there are some types that don't have a defined
132ordering relation. For example, ``3+4j < 5+7j`` isn't a valid
133comparison.
134
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136.. _tut-lists-as-stacks:
137
138Using Lists as Stacks
139---------------------
140
141.. sectionauthor:: Ka-Ping Yee <ping@lfw.org>
142
143
144The list methods make it very easy to use a list as a stack, where the last
145element added is the first element retrieved ("last-in, first-out"). To add an
146item to the top of the stack, use :meth:`append`. To retrieve an item from the
147top 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
168Using Lists as Queues
169---------------------
170
171.. sectionauthor:: Ka-Ping Yee <ping@lfw.org>
172
Ezio Melotti8f8db142010-03-31 07:45:32 +0000173It is also possible to use a list as a queue, where the first element added is
174the first element retrieved ("first-in, first-out"); however, lists are not
175efficient for this purpose. While appends and pops from the end of list are
176fast, doing inserts or pops from the beginning of a list is slow (because all
177of the other elements have to be shifted by one).
Georg Brandl116aa622007-08-15 14:28:22 +0000178
Ezio Melotti8f8db142010-03-31 07:45:32 +0000179To implement a queue, use :class:`collections.deque` which was designed to
180have fast appends and pops from both ends. For example::
Georg Brandl116aa622007-08-15 14:28:22 +0000181
Ezio Melotti8f8db142010-03-31 07:45:32 +0000182 >>> from collections import deque
183 >>> queue = deque(["Eric", "John", "Michael"])
Georg Brandl116aa622007-08-15 14:28:22 +0000184 >>> queue.append("Terry") # Terry arrives
185 >>> queue.append("Graham") # Graham arrives
Ezio Melotti8f8db142010-03-31 07:45:32 +0000186 >>> queue.popleft() # The first to arrive now leaves
Georg Brandl116aa622007-08-15 14:28:22 +0000187 'Eric'
Ezio Melotti8f8db142010-03-31 07:45:32 +0000188 >>> queue.popleft() # The second to arrive now leaves
Georg Brandl116aa622007-08-15 14:28:22 +0000189 'John'
Ezio Melotti8f8db142010-03-31 07:45:32 +0000190 >>> queue # Remaining queue in order of arrival
191 deque(['Michael', 'Terry', 'Graham'])
Georg Brandl718ce2c2010-03-21 09:51:44 +0000192
Georg Brandl116aa622007-08-15 14:28:22 +0000193
Georg Brandlfc11f272009-06-16 19:22:10 +0000194.. _tut-listcomps:
195
Georg Brandl116aa622007-08-15 14:28:22 +0000196List Comprehensions
197-------------------
198
Ezio Melotti91621e22011-12-13 15:36:19 +0200199List comprehensions provide a concise way to create lists.
200Common applications are to make new lists where each element is the result of
201some operations applied to each member of another sequence or iterable, or to
202create a subsequence of those elements that satisfy a certain condition.
203
204For 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 Murray6bd68602014-09-30 21:25:38 -0400213Note that this creates (or overwrites) a variable named ``x`` that still exists
214after the loop completes. We can calculate the list of squares without any
215side effects using::
216
217 squares = list(map(lambda x: x**2, range(10)))
218
219or, equivalently::
Ezio Melotti91621e22011-12-13 15:36:19 +0200220
221 squares = [x**2 for x in range(10)]
222
R David Murray6bd68602014-09-30 21:25:38 -0400223which is more concise and readable.
Guido van Rossum0616b792007-08-31 03:25:11 +0000224
Georg Brandl7ae90dd2009-06-08 18:59:09 +0000225A list comprehension consists of brackets containing an expression followed
Serhiy Storchaka2b57c432018-12-19 08:09:46 +0200226by a :keyword:`!for` clause, then zero or more :keyword:`!for` or :keyword:`!if`
Ezio Melotti91621e22011-12-13 15:36:19 +0200227clauses. The result will be a new list resulting from evaluating the expression
Serhiy Storchaka2b57c432018-12-19 08:09:46 +0200228in the context of the :keyword:`!for` and :keyword:`!if` clauses which follow it.
Ezio Melotti91621e22011-12-13 15:36:19 +0200229For example, this listcomp combines the elements of two lists if they are not
230equal::
Guido van Rossum0616b792007-08-31 03:25:11 +0000231
Ezio Melotti91621e22011-12-13 15:36:19 +0200232 >>> [(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 Rossum0616b792007-08-31 03:25:11 +0000234
Ezio Melotti91621e22011-12-13 15:36:19 +0200235and it's equivalent to::
Guido van Rossum0616b792007-08-31 03:25:11 +0000236
Ezio Melotti91621e22011-12-13 15:36:19 +0200237 >>> 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 Rossum0616b792007-08-31 03:25:11 +0000245
Ezio Melotti91621e22011-12-13 15:36:19 +0200246Note how the order of the :keyword:`for` and :keyword:`if` statements is the
247same in both these snippets.
Guido van Rossum0616b792007-08-31 03:25:11 +0000248
Ezio Melotti91621e22011-12-13 15:36:19 +0200249If the expression is a tuple (e.g. the ``(x, y)`` in the previous example),
250it must be parenthesized. ::
Georg Brandl116aa622007-08-15 14:28:22 +0000251
Ezio Melotti91621e22011-12-13 15:36:19 +0200252 >>> 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 Brandl116aa622007-08-15 14:28:22 +0000263 >>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
264 >>> [weapon.strip() for weapon in freshfruit]
265 ['banana', 'loganberry', 'passion fruit']
Ezio Melotti91621e22011-12-13 15:36:19 +0200266 >>> # 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)]
UltimateCoder88569402017-05-03 22:16:45 +0530271 File "<stdin>", line 1, in <module>
Ezio Melotti91621e22011-12-13 15:36:19 +0200272 [x, x**2 for x in range(6)]
Georg Brandl116aa622007-08-15 14:28:22 +0000273 ^
274 SyntaxError: invalid syntax
Ezio Melotti91621e22011-12-13 15:36:19 +0200275 >>> # 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 Rossum0616b792007-08-31 03:25:11 +0000279
Ezio Melotti91621e22011-12-13 15:36:19 +0200280List comprehensions can contain complex expressions and nested functions::
Guido van Rossum0616b792007-08-31 03:25:11 +0000281
Ezio Melotti91621e22011-12-13 15:36:19 +0200282 >>> from math import pi
283 >>> [str(round(pi, i)) for i in range(1, 6)]
Georg Brandl116aa622007-08-15 14:28:22 +0000284 ['3.1', '3.14', '3.142', '3.1416', '3.14159']
285
Christian Heimes0449f632007-12-15 01:27:15 +0000286Nested List Comprehensions
287--------------------------
288
Ezio Melotti91621e22011-12-13 15:36:19 +0200289The initial expression in a list comprehension can be any arbitrary expression,
290including another list comprehension.
Christian Heimes0449f632007-12-15 01:27:15 +0000291
Ezio Melotti91621e22011-12-13 15:36:19 +0200292Consider the following example of a 3x4 matrix implemented as a list of
2933 lists of length 4::
Christian Heimes0449f632007-12-15 01:27:15 +0000294
Ezio Melotti91621e22011-12-13 15:36:19 +0200295 >>> matrix = [
296 ... [1, 2, 3, 4],
297 ... [5, 6, 7, 8],
298 ... [9, 10, 11, 12],
299 ... ]
Christian Heimes0449f632007-12-15 01:27:15 +0000300
Ezio Melotti91621e22011-12-13 15:36:19 +0200301The following list comprehension will transpose rows and columns::
Christian Heimes0449f632007-12-15 01:27:15 +0000302
Ezio Melotti91621e22011-12-13 15:36:19 +0200303 >>> [[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 Heimes0449f632007-12-15 01:27:15 +0000305
Ezio Melotti91621e22011-12-13 15:36:19 +0200306As we saw in the previous section, the nested listcomp is evaluated in
307the context of the :keyword:`for` that follows it, so this example is
308equivalent to::
Christian Heimes0449f632007-12-15 01:27:15 +0000309
Ezio Melotti91621e22011-12-13 15:36:19 +0200310 >>> 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 Heimes0449f632007-12-15 01:27:15 +0000316
Ezio Melotti91621e22011-12-13 15:36:19 +0200317which, in turn, is the same as::
Christian Heimes0449f632007-12-15 01:27:15 +0000318
Ezio Melotti91621e22011-12-13 15:36:19 +0200319 >>> 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 Heimes0449f632007-12-15 01:27:15 +0000329
Ezio Melotti91621e22011-12-13 15:36:19 +0200330In the real world, you should prefer built-in functions to complex flow statements.
Christian Heimes0449f632007-12-15 01:27:15 +0000331The :func:`zip` function would do a great job for this use case::
332
Sandro Tosi0a90a822012-08-12 10:24:50 +0200333 >>> list(zip(*matrix))
Ezio Melotti91621e22011-12-13 15:36:19 +0200334 [(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
Christian Heimes0449f632007-12-15 01:27:15 +0000335
336See :ref:`tut-unpacking-arguments` for details on the asterisk in this line.
337
Georg Brandl116aa622007-08-15 14:28:22 +0000338.. _tut-del:
339
Serhiy Storchaka2b57c432018-12-19 08:09:46 +0200340The :keyword:`!del` statement
341=============================
Georg Brandl116aa622007-08-15 14:28:22 +0000342
343There is a way to remove an item from a list given its index instead of its
344value: the :keyword:`del` statement. This differs from the :meth:`pop` method
Serhiy Storchaka2b57c432018-12-19 08:09:46 +0200345which returns a value. The :keyword:`!del` statement can also be used to remove
Georg Brandl116aa622007-08-15 14:28:22 +0000346slices from a list or clear the entire list (which we did earlier by assignment
347of 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
364Referencing the name ``a`` hereafter is an error (at least until another value
365is assigned to it). We'll find other uses for :keyword:`del` later.
366
367
Georg Brandl5d955ed2008-09-13 17:18:21 +0000368.. _tut-tuples:
Georg Brandl116aa622007-08-15 14:28:22 +0000369
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000370Tuples and Sequences
371====================
372
373We saw that lists and strings have many common properties, such as indexing and
374slicing operations. They are two examples of *sequence* data types (see
375:ref:`typesseq`). Since Python is an evolving language, other sequence data
376types may be added. There is also another standard sequence data type: the
377*tuple*.
378
379A 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 Melottif90ea1f2012-06-17 14:10:59 +0200390 >>> # 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 Heimes5b5e81c2007-12-31 16:14:33 +0000400
401As you see, on output tuples are always enclosed in parentheses, so that nested
402tuples are interpreted correctly; they may be input with or without surrounding
403parentheses, although often parentheses are necessary anyway (if the tuple is
Ezio Melottif90ea1f2012-06-17 14:10:59 +0200404part of a larger expression). It is not possible to assign to the individual
405items of a tuple, however it is possible to create tuples which contain mutable
406objects, such as lists.
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000407
Ezio Melottif90ea1f2012-06-17 14:10:59 +0200408Though tuples may seem similar to lists, they are often used in different
409situations and for different purposes.
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300410Tuples are :term:`immutable`, and usually contain a heterogeneous sequence of
Ezio Melottif90ea1f2012-06-17 14:10:59 +0200411elements 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>`).
413Lists are :term:`mutable`, and their elements are usually homogeneous and are
414accessed by iterating over the list.
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000415
416A special problem is the construction of tuples containing 0 or 1 items: the
417syntax has some extra quirks to accommodate these. Empty tuples are constructed
418by an empty pair of parentheses; a tuple with one item is constructed by
419following a value with a comma (it is not sufficient to enclose a single value
420in 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
431The statement ``t = 12345, 54321, 'hello!'`` is an example of *tuple packing*:
432the values ``12345``, ``54321`` and ``'hello!'`` are packed together in a tuple.
433The reverse operation is also possible::
434
435 >>> x, y, z = t
436
Benjamin Petersond23f8222009-04-05 19:13:16 +0000437This is called, appropriately enough, *sequence unpacking* and works for any
Georg Brandl7ae90dd2009-06-08 18:59:09 +0000438sequence on the right-hand side. Sequence unpacking requires that there are as
439many variables on the left side of the equals sign as there are elements in the
Benjamin Petersond23f8222009-04-05 19:13:16 +0000440sequence. Note that multiple assignment is really just a combination of tuple
441packing and sequence unpacking.
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000442
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000443
Georg Brandl116aa622007-08-15 14:28:22 +0000444.. _tut-sets:
445
446Sets
447====
448
449Python also includes a data type for *sets*. A set is an unordered collection
450with no duplicate elements. Basic uses include membership testing and
451eliminating duplicate entries. Set objects also support mathematical operations
452like union, intersection, difference, and symmetric difference.
453
Ezio Melotti89b03b02012-11-17 12:06:01 +0200454Curly braces or the :func:`set` function can be used to create sets. Note: to
Georg Brandl10e0e302009-06-08 20:25:55 +0000455create an empty set you have to use ``set()``, not ``{}``; the latter creates an
456empty dictionary, a data structure that we discuss in the next section.
Guido van Rossum0616b792007-08-31 03:25:11 +0000457
Georg Brandl116aa622007-08-15 14:28:22 +0000458Here is a brief demonstration::
459
Raymond Hettingerafdeca92010-08-08 01:30:45 +0000460 >>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
461 >>> print(basket) # show that duplicates have been removed
Georg Brandl1790ed22010-11-10 07:57:10 +0000462 {'orange', 'banana', 'pear', 'apple'}
Raymond Hettingerafdeca92010-08-08 01:30:45 +0000463 >>> 'orange' in basket # fast membership testing
Georg Brandl116aa622007-08-15 14:28:22 +0000464 True
Raymond Hettingerafdeca92010-08-08 01:30:45 +0000465 >>> 'crabgrass' in basket
Georg Brandl116aa622007-08-15 14:28:22 +0000466 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 Rossum0616b792007-08-31 03:25:11 +0000473 {'a', 'r', 'b', 'c', 'd'}
Georg Brandl116aa622007-08-15 14:28:22 +0000474 >>> a - b # letters in a but not in b
Guido van Rossum0616b792007-08-31 03:25:11 +0000475 {'r', 'd', 'b'}
KatherineMichelca816152017-06-10 14:19:09 -0500476 >>> a | b # letters in a or b or both
Guido van Rossum0616b792007-08-31 03:25:11 +0000477 {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
Georg Brandl116aa622007-08-15 14:28:22 +0000478 >>> a & b # letters in both a and b
Guido van Rossum0616b792007-08-31 03:25:11 +0000479 {'a', 'c'}
Georg Brandl116aa622007-08-15 14:28:22 +0000480 >>> a ^ b # letters in a or b but not both
Guido van Rossum0616b792007-08-31 03:25:11 +0000481 {'r', 'd', 'b', 'm', 'z', 'l'}
482
Ezio Melotti89b03b02012-11-17 12:06:01 +0200483Similarly to :ref:`list comprehensions <tut-listcomps>`, set comprehensions
484are also supported::
Georg Brandlf6945182008-02-01 11:56:49 +0000485
486 >>> a = {x for x in 'abracadabra' if x not in 'abc'}
487 >>> a
488 {'r', 'd'}
Guido van Rossum0616b792007-08-31 03:25:11 +0000489
Georg Brandl116aa622007-08-15 14:28:22 +0000490
Georg Brandl116aa622007-08-15 14:28:22 +0000491.. _tut-dictionaries:
492
493Dictionaries
494============
495
496Another 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
499indexed by a range of numbers, dictionaries are indexed by *keys*, which can be
500any immutable type; strings and numbers can always be keys. Tuples can be used
501as keys if they contain only strings, numbers, or tuples; if a tuple contains
502any mutable object either directly or indirectly, it cannot be used as a key.
503You can't use lists as keys, since lists can be modified in place using index
504assignments, slice assignments, or methods like :meth:`append` and
505:meth:`extend`.
506
hui shangdfbbbf12018-04-04 12:55:05 +0800507It is best to think of a dictionary as a set of *key: value* pairs,
Georg Brandl116aa622007-08-15 14:28:22 +0000508with the requirement that the keys are unique (within one dictionary). A pair of
509braces creates an empty dictionary: ``{}``. Placing a comma-separated list of
510key:value pairs within the braces adds initial key:value pairs to the
511dictionary; this is also the way dictionaries are written on output.
512
513The main operations on a dictionary are storing a value with some key and
514extracting the value given the key. It is also possible to delete a key:value
515pair with ``del``. If you store using a key that is already in use, the old
516value associated with that key is forgotten. It is an error to extract a value
517using a non-existent key.
518
hui shangdfbbbf12018-04-04 12:55:05 +0800519Performing ``list(d)`` on a dictionary returns a list of all the keys
520used 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 Brandlfc11f272009-06-16 19:22:10 +0000522dictionary, use the :keyword:`in` keyword.
Georg Brandl116aa622007-08-15 14:28:22 +0000523
524Here is a small example using a dictionary::
525
526 >>> tel = {'jack': 4098, 'sape': 4139}
527 >>> tel['guido'] = 4127
528 >>> tel
hui shangdfbbbf12018-04-04 12:55:05 +0800529 {'jack': 4098, 'sape': 4139, 'guido': 4127}
Georg Brandl116aa622007-08-15 14:28:22 +0000530 >>> tel['jack']
531 4098
532 >>> del tel['sape']
533 >>> tel['irv'] = 4127
534 >>> tel
hui shangdfbbbf12018-04-04 12:55:05 +0800535 {'jack': 4098, 'guido': 4127, 'irv': 4127}
536 >>> list(tel)
537 ['jack', 'guido', 'irv']
538 >>> sorted(tel)
Georg Brandl116aa622007-08-15 14:28:22 +0000539 ['guido', 'irv', 'jack']
Georg Brandl116aa622007-08-15 14:28:22 +0000540 >>> 'guido' in tel
541 True
Neal Norwitze0906d12007-08-31 03:46:28 +0000542 >>> 'jack' not in tel
543 False
Georg Brandl116aa622007-08-15 14:28:22 +0000544
Georg Brandlfc11f272009-06-16 19:22:10 +0000545The :func:`dict` constructor builds dictionaries directly from sequences of
Raymond Hettinger8699aea2009-06-16 20:49:30 +0000546key-value pairs::
Georg Brandl116aa622007-08-15 14:28:22 +0000547
548 >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
hui shangdfbbbf12018-04-04 12:55:05 +0800549 {'sape': 4139, 'guido': 4127, 'jack': 4098}
Georg Brandl116aa622007-08-15 14:28:22 +0000550
Georg Brandlf6945182008-02-01 11:56:49 +0000551In addition, dict comprehensions can be used to create dictionaries from
552arbitrary key and value expressions::
553
554 >>> {x: x**2 for x in (2, 4, 6)}
555 {2: 4, 4: 16, 6: 36}
Georg Brandl116aa622007-08-15 14:28:22 +0000556
557When the keys are simple strings, it is sometimes easier to specify pairs using
558keyword arguments::
559
560 >>> dict(sape=4139, guido=4127, jack=4098)
hui shangdfbbbf12018-04-04 12:55:05 +0800561 {'sape': 4139, 'guido': 4127, 'jack': 4098}
Georg Brandl116aa622007-08-15 14:28:22 +0000562
563
564.. _tut-loopidioms:
565
566Looping Techniques
567==================
568
569When looping through dictionaries, the key and corresponding value can be
Neal Norwitze0906d12007-08-31 03:46:28 +0000570retrieved at the same time using the :meth:`items` method. ::
Georg Brandl116aa622007-08-15 14:28:22 +0000571
572 >>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
Neal Norwitze0906d12007-08-31 03:46:28 +0000573 >>> for k, v in knights.items():
Guido van Rossum0616b792007-08-31 03:25:11 +0000574 ... print(k, v)
Georg Brandl116aa622007-08-15 14:28:22 +0000575 ...
576 gallahad the pure
577 robin the brave
578
579When looping through a sequence, the position index and corresponding value can
580be retrieved at the same time using the :func:`enumerate` function. ::
581
582 >>> for i, v in enumerate(['tic', 'tac', 'toe']):
Guido van Rossum0616b792007-08-31 03:25:11 +0000583 ... print(i, v)
Georg Brandl116aa622007-08-15 14:28:22 +0000584 ...
585 0 tic
586 1 tac
587 2 toe
588
589To loop over two or more sequences at the same time, the entries can be paired
590with 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 Petersone6f00632008-05-26 01:03:56 +0000595 ... print('What is your {0}? It is {1}.'.format(q, a))
Georg Brandl06788c92009-01-03 21:31:47 +0000596 ...
Georg Brandl116aa622007-08-15 14:28:22 +0000597 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
601To loop over a sequence in reverse, first specify the sequence in a forward
602direction and then call the :func:`reversed` function. ::
603
Georg Brandle4ac7502007-09-03 07:10:24 +0000604 >>> for i in reversed(range(1, 10, 2)):
Guido van Rossum0616b792007-08-31 03:25:11 +0000605 ... print(i)
Georg Brandl116aa622007-08-15 14:28:22 +0000606 ...
607 9
608 7
609 5
610 3
611 1
612
613To loop over a sequence in sorted order, use the :func:`sorted` function which
614returns a new sorted list while leaving the source unaltered. ::
615
616 >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
Rahul Kumaresaneefd4e02020-05-18 07:02:34 +0530617 >>> for i in sorted(basket):
618 ... print(i)
619 ...
620 apple
621 apple
622 banana
623 orange
624 orange
625 pear
626
627Using :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
629way to loop over unique elements of the sequence in sorted order. ::
630
631 >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
Georg Brandl116aa622007-08-15 14:28:22 +0000632 >>> for f in sorted(set(basket)):
Guido van Rossum0616b792007-08-31 03:25:11 +0000633 ... print(f)
Georg Brandl06788c92009-01-03 21:31:47 +0000634 ...
Georg Brandl116aa622007-08-15 14:28:22 +0000635 apple
636 banana
637 orange
638 pear
639
Raymond Hettinger502bf512015-09-01 02:33:02 -0700640It is sometimes tempting to change a list while you are looping over it;
641however, it is often simpler and safer to create a new list instead. ::
Chris Jerdonek4fab8f02012-10-15 19:44:47 -0700642
Raymond Hettinger502bf512015-09-01 02:33:02 -0700643 >>> 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 Jerdonek4fab8f02012-10-15 19:44:47 -0700649 ...
Raymond Hettinger502bf512015-09-01 02:33:02 -0700650 >>> filtered_data
651 [56.2, 51.7, 55.3, 52.5, 47.8]
Chris Jerdonek4fab8f02012-10-15 19:44:47 -0700652
Georg Brandl116aa622007-08-15 14:28:22 +0000653
654.. _tut-conditions:
655
656More on Conditions
657==================
658
659The conditions used in ``while`` and ``if`` statements can contain any
660operators, not just comparisons.
661
662The 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
664whether two objects are really the same object; this only matters for mutable
665objects like lists. All comparison operators have the same priority, which is
666lower than that of all numerical operators.
667
668Comparisons can be chained. For example, ``a < b == c`` tests whether ``a`` is
669less than ``b`` and moreover ``b`` equals ``c``.
670
671Comparisons may be combined using the Boolean operators ``and`` and ``or``, and
672the outcome of a comparison (or of any other Boolean expression) may be negated
673with ``not``. These have lower priorities than comparison operators; between
674them, ``not`` has the highest priority and ``or`` the lowest, so that ``A and
675not B or C`` is equivalent to ``(A and (not B)) or C``. As always, parentheses
676can be used to express the desired composition.
677
678The Boolean operators ``and`` and ``or`` are so-called *short-circuit*
679operators: their arguments are evaluated from left to right, and evaluation
680stops as soon as the outcome is determined. For example, if ``A`` and ``C`` are
681true 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
683short-circuit operator is the last evaluated argument.
684
685It is possible to assign the result of a comparison or other Boolean expression
686to 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 Askarcb2cf062019-10-25 18:20:05 -0400693Note that in Python, unlike C, assignment inside expressions must be done
Adorilson Bezerra5807efd2020-02-03 14:11:19 -0300694explicitly with the
695:ref:`walrus operator <why-can-t-i-use-an-assignment-in-an-expression>` ``:=``.
696This avoids a common class of problems encountered in C programs: typing ``=``
697in an expression when ``==`` was intended.
Georg Brandl116aa622007-08-15 14:28:22 +0000698
699
700.. _tut-comparing:
701
702Comparing Sequences and Other Types
703===================================
Emmanuel Ariasb00479d2019-04-02 01:52:42 -0300704Sequence objects typically may be compared to other objects with the same sequence
705type. The comparison uses *lexicographical* ordering: first the first two
706items are compared, and if they differ this determines the outcome of the
707comparison; if they are equal, the next two items are compared, and so on, until
708either sequence is exhausted. If two items to be compared are themselves
709sequences of the same type, the lexicographical comparison is carried out
710recursively. If all items of two sequences compare equal, the sequences are
711considered equal. If one sequence is an initial sub-sequence of the other, the
712shorter sequence is the smaller (lesser) one. Lexicographical ordering for
713strings uses the Unicode code point number to order individual characters.
714Some examples of comparisons between sequences of the same type::
Georg Brandl116aa622007-08-15 14:28:22 +0000715
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 Brandl9f2c39a2007-10-08 14:08:36 +0000724Note that comparing objects of different types with ``<`` or ``>`` is legal
725provided that the objects have appropriate comparison methods. For example,
726mixed numeric types are compared according to their numeric value, so 0 equals
7270.0, etc. Otherwise, rather than providing an arbitrary ordering, the
728interpreter will raise a :exc:`TypeError` exception.
Georg Brandlfc11f272009-06-16 19:22:10 +0000729
730
731.. rubric:: Footnotes
732
Georg Brandl388349a2011-10-08 18:32:40 +0200733.. [1] Other languages may return the mutated object, which allows method
734 chaining, such as ``d->insert("a")->remove("b")->sort();``.