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