blob: 0b45fe123dff3154abb6e0e657816cae6ce57cf5 [file] [log] [blame]
Raymond Hettingere5820c62011-03-22 09:11:39 -07001
Raymond Hettinger53dbe392008-02-12 20:03:09 +00002:mod:`collections` --- Container datatypes
3==========================================
Georg Brandl116aa622007-08-15 14:28:22 +00004
5.. module:: collections
Raymond Hettinger53dbe392008-02-12 20:03:09 +00006 :synopsis: Container datatypes
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Raymond Hettinger <python@rcn.com>
8.. sectionauthor:: Raymond Hettinger <python@rcn.com>
9
Christian Heimesfe337bf2008-03-23 21:54:12 +000010.. testsetup:: *
11
12 from collections import *
13 import itertools
14 __name__ = '<doctest>'
Georg Brandl116aa622007-08-15 14:28:22 +000015
Raymond Hettingera1993682011-01-27 01:20:32 +000016**Source code:** :source:`Lib/collections.py` and :source:`Lib/_abcoll.py`
Raymond Hettinger10480942011-01-10 03:26:08 +000017
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000018--------------
19
Raymond Hettingera6b76ba2010-08-08 00:29:08 +000020This module implements specialized container datatypes providing alternatives to
21Python's general purpose built-in containers, :class:`dict`, :class:`list`,
22:class:`set`, and :class:`tuple`.
Christian Heimes0bd4e112008-02-12 22:59:25 +000023
Raymond Hettingera6b76ba2010-08-08 00:29:08 +000024===================== ====================================================================
25:func:`namedtuple` factory function for creating tuple subclasses with named fields
26:class:`deque` list-like container with fast appends and pops on either end
27:class:`Counter` dict subclass for counting hashable objects
28:class:`OrderedDict` dict subclass that remembers the order entries were added
29:class:`defaultdict` dict subclass that calls a factory function to supply missing values
30:class:`UserDict` wrapper around dictionary objects for easier dict subclassing
31:class:`UserList` wrapper around list objects for easier list subclassing
32:class:`UserString` wrapper around string objects for easier string subclassing
33===================== ====================================================================
Georg Brandl116aa622007-08-15 14:28:22 +000034
Raymond Hettingera6b76ba2010-08-08 00:29:08 +000035In addition to the concrete container classes, the collections module provides
Éric Araujofa088db2011-06-04 18:42:38 +020036:ref:`abstract base classes <collections-abstract-base-classes>` that can be
37used to test whether a class provides a particular interface, for example,
38whether it is hashable or a mapping.
Mark Summerfield08898b42007-09-05 08:43:04 +000039
40
Raymond Hettingerb8baf632009-01-14 02:20:07 +000041:class:`Counter` objects
42------------------------
43
44A counter tool is provided to support convenient and rapid tallies.
45For example::
46
Raymond Hettinger1c62dc92009-02-04 11:41:45 +000047 >>> # Tally occurrences of words in a list
Raymond Hettingerb8baf632009-01-14 02:20:07 +000048 >>> cnt = Counter()
Raymond Hettinger670eaec2009-01-21 23:14:07 +000049 >>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
Raymond Hettingerb8baf632009-01-14 02:20:07 +000050 ... cnt[word] += 1
51 >>> cnt
52 Counter({'blue': 3, 'red': 2, 'green': 1})
53
Raymond Hettinger1c62dc92009-02-04 11:41:45 +000054 >>> # Find the ten most common words in Hamlet
Raymond Hettingerb8baf632009-01-14 02:20:07 +000055 >>> import re
56 >>> words = re.findall('\w+', open('hamlet.txt').read().lower())
Raymond Hettinger0bae6622009-01-20 13:00:59 +000057 >>> Counter(words).most_common(10)
Raymond Hettingerb8baf632009-01-14 02:20:07 +000058 [('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
59 ('you', 554), ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
60
61.. class:: Counter([iterable-or-mapping])
62
Raymond Hettinger670eaec2009-01-21 23:14:07 +000063 A :class:`Counter` is a :class:`dict` subclass for counting hashable objects.
Raymond Hettingerb8baf632009-01-14 02:20:07 +000064 It is an unordered collection where elements are stored as dictionary keys
65 and their counts are stored as dictionary values. Counts are allowed to be
66 any integer value including zero or negative counts. The :class:`Counter`
67 class is similar to bags or multisets in other languages.
68
69 Elements are counted from an *iterable* or initialized from another
Benjamin Peterson25c95f12009-05-08 20:42:26 +000070 *mapping* (or counter):
Raymond Hettingerb8baf632009-01-14 02:20:07 +000071
Raymond Hettinger73662a52009-01-27 02:38:22 +000072 >>> c = Counter() # a new, empty counter
73 >>> c = Counter('gallahad') # a new counter from an iterable
74 >>> c = Counter({'red': 4, 'blue': 2}) # a new counter from a mapping
75 >>> c = Counter(cats=4, dogs=8) # a new counter from keyword args
Raymond Hettingerb8baf632009-01-14 02:20:07 +000076
Raymond Hettinger670eaec2009-01-21 23:14:07 +000077 Counter objects have a dictionary interface except that they return a zero
Benjamin Peterson25c95f12009-05-08 20:42:26 +000078 count for missing items instead of raising a :exc:`KeyError`:
Raymond Hettingerb8baf632009-01-14 02:20:07 +000079
Raymond Hettinger94adc8e2009-01-22 05:27:37 +000080 >>> c = Counter(['eggs', 'ham'])
Raymond Hettingerb8baf632009-01-14 02:20:07 +000081 >>> c['bacon'] # count of a missing element is zero
82 0
83
Raymond Hettinger94adc8e2009-01-22 05:27:37 +000084 Setting a count to zero does not remove an element from a counter.
85 Use ``del`` to remove it entirely:
Raymond Hettingerb8baf632009-01-14 02:20:07 +000086
Raymond Hettinger94adc8e2009-01-22 05:27:37 +000087 >>> c['sausage'] = 0 # counter entry with a zero count
88 >>> del c['sausage'] # del actually removes the entry
Raymond Hettingerb8baf632009-01-14 02:20:07 +000089
Benjamin Petersond45bf582009-03-02 21:44:54 +000090 .. versionadded:: 3.1
Raymond Hettingerb8baf632009-01-14 02:20:07 +000091
92
Ezio Melotti0be8b1c2010-04-04 06:53:44 +000093 Counter objects support three methods beyond those available for all
Raymond Hettingerb8baf632009-01-14 02:20:07 +000094 dictionaries:
95
96 .. method:: elements()
97
Raymond Hettinger670eaec2009-01-21 23:14:07 +000098 Return an iterator over elements repeating each as many times as its
99 count. Elements are returned in arbitrary order. If an element's count
100 is less than one, :meth:`elements` will ignore it.
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000101
Raymond Hettinger0bae6622009-01-20 13:00:59 +0000102 >>> c = Counter(a=4, b=2, c=0, d=-2)
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000103 >>> list(c.elements())
104 ['a', 'a', 'a', 'a', 'b', 'b']
105
106 .. method:: most_common([n])
107
Raymond Hettinger73662a52009-01-27 02:38:22 +0000108 Return a list of the *n* most common elements and their counts from the
Raymond Hettingerd04fa312009-02-04 19:45:13 +0000109 most common to the least. If *n* is not specified, :func:`most_common`
Raymond Hettinger73662a52009-01-27 02:38:22 +0000110 returns *all* elements in the counter. Elements with equal counts are
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000111 ordered arbitrarily:
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000112
113 >>> Counter('abracadabra').most_common(3)
114 [('a', 5), ('r', 2), ('b', 2)]
115
Raymond Hettinger9c01e442010-04-03 10:32:58 +0000116 .. method:: subtract([iterable-or-mapping])
117
118 Elements are subtracted from an *iterable* or from another *mapping*
119 (or counter). Like :meth:`dict.update` but subtracts counts instead
120 of replacing them. Both inputs and outputs may be zero or negative.
121
122 >>> c = Counter(a=4, b=2, c=0, d=-2)
123 >>> d = Counter(a=1, b=2, c=3, d=4)
124 >>> c.subtract(d)
125 Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})
126
Ezio Melotti0be8b1c2010-04-04 06:53:44 +0000127 .. versionadded:: 3.2
128
Raymond Hettinger670eaec2009-01-21 23:14:07 +0000129 The usual dictionary methods are available for :class:`Counter` objects
130 except for two which work differently for counters.
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000131
132 .. method:: fromkeys(iterable)
133
Raymond Hettinger73662a52009-01-27 02:38:22 +0000134 This class method is not implemented for :class:`Counter` objects.
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000135
136 .. method:: update([iterable-or-mapping])
137
Raymond Hettinger73662a52009-01-27 02:38:22 +0000138 Elements are counted from an *iterable* or added-in from another
139 *mapping* (or counter). Like :meth:`dict.update` but adds counts
140 instead of replacing them. Also, the *iterable* is expected to be a
141 sequence of elements, not a sequence of ``(key, value)`` pairs.
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000142
143Common patterns for working with :class:`Counter` objects::
144
Raymond Hettinger73662a52009-01-27 02:38:22 +0000145 sum(c.values()) # total of all counts
146 c.clear() # reset all counts
147 list(c) # list unique elements
148 set(c) # convert to a set
149 dict(c) # convert to a regular dictionary
150 c.items() # convert to a list of (elem, cnt) pairs
151 Counter(dict(list_of_pairs)) # convert from a list of (elem, cnt) pairs
152 c.most_common()[:-n:-1] # n least common elements
153 c += Counter() # remove zero and negative counts
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000154
Raymond Hettinger72a95cc2009-02-25 22:51:40 +0000155Several mathematical operations are provided for combining :class:`Counter`
156objects to produce multisets (counters that have counts greater than zero).
157Addition and subtraction combine counters by adding or subtracting the counts
158of corresponding elements. Intersection and union return the minimum and
159maximum of corresponding counts. Each operation can accept inputs with signed
160counts, but the output will exclude results with counts of zero or less.
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000161
Raymond Hettingere0d1b9f2009-01-21 20:36:27 +0000162 >>> c = Counter(a=3, b=1)
163 >>> d = Counter(a=1, b=2)
Raymond Hettinger73662a52009-01-27 02:38:22 +0000164 >>> c + d # add two counters together: c[x] + d[x]
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000165 Counter({'a': 4, 'b': 3})
Raymond Hettinger73662a52009-01-27 02:38:22 +0000166 >>> c - d # subtract (keeping only positive counts)
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000167 Counter({'a': 2})
Raymond Hettinger73662a52009-01-27 02:38:22 +0000168 >>> c & d # intersection: min(c[x], d[x])
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000169 Counter({'a': 1, 'b': 1})
Raymond Hettinger73662a52009-01-27 02:38:22 +0000170 >>> c | d # union: max(c[x], d[x])
Raymond Hettinger4d2073a2009-01-20 03:41:22 +0000171 Counter({'a': 3, 'b': 2})
172
Raymond Hettinger22f18852010-04-12 21:45:14 +0000173.. note::
174
175 Counters were primarily designed to work with positive integers to represent
176 running counts; however, care was taken to not unnecessarily preclude use
177 cases needing other types or negative values. To help with those use cases,
178 this section documents the minimum range and type restrictions.
179
180 * The :class:`Counter` class itself is a dictionary subclass with no
181 restrictions on its keys and values. The values are intended to be numbers
182 representing counts, but you *could* store anything in the value field.
183
184 * The :meth:`most_common` method requires only that the values be orderable.
185
186 * For in-place operations such as ``c[key] += 1``, the value type need only
187 support addition and subtraction. So fractions, floats, and decimals would
188 work and negative values are supported. The same is also true for
189 :meth:`update` and :meth:`subtract` which allow negative and zero values
190 for both inputs and outputs.
191
192 * The multiset methods are designed only for use cases with positive values.
193 The inputs may be negative or zero, but only outputs with positive values
194 are created. There are no type restrictions, but the value type needs to
195 support support addition, subtraction, and comparison.
196
197 * The :meth:`elements` method requires integer counts. It ignores zero and
198 negative counts.
199
Raymond Hettingerb14043c2009-01-20 23:44:31 +0000200.. seealso::
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000201
Raymond Hettinger94adc8e2009-01-22 05:27:37 +0000202 * `Counter class <http://code.activestate.com/recipes/576611/>`_
203 adapted for Python 2.5 and an early `Bag recipe
204 <http://code.activestate.com/recipes/259174/>`_ for Python 2.4.
205
Raymond Hettingerb14043c2009-01-20 23:44:31 +0000206 * `Bag class <http://www.gnu.org/software/smalltalk/manual-base/html_node/Bag.html>`_
207 in Smalltalk.
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000208
Éric Araujo08c9bd52011-04-24 02:59:02 +0200209 * Wikipedia entry for `Multisets <http://en.wikipedia.org/wiki/Multiset>`_.
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000210
Raymond Hettingerb14043c2009-01-20 23:44:31 +0000211 * `C++ multisets <http://www.demo2s.com/Tutorial/Cpp/0380__set-multiset/Catalog0380__set-multiset.htm>`_
Raymond Hettinger94adc8e2009-01-22 05:27:37 +0000212 tutorial with examples.
Raymond Hettingerb14043c2009-01-20 23:44:31 +0000213
Raymond Hettinger94adc8e2009-01-22 05:27:37 +0000214 * For mathematical operations on multisets and their use cases, see
Raymond Hettingerb14043c2009-01-20 23:44:31 +0000215 *Knuth, Donald. The Art of Computer Programming Volume II,
Éric Araujo08c9bd52011-04-24 02:59:02 +0200216 Section 4.6.3, Exercise 19*.
Raymond Hettingerb14043c2009-01-20 23:44:31 +0000217
Raymond Hettinger670eaec2009-01-21 23:14:07 +0000218 * To enumerate all distinct multisets of a given size over a given set of
Raymond Hettingerd07d9392009-01-27 04:20:44 +0000219 elements, see :func:`itertools.combinations_with_replacement`.
Raymond Hettingerb14043c2009-01-20 23:44:31 +0000220
Raymond Hettinger94adc8e2009-01-22 05:27:37 +0000221 map(Counter, combinations_with_replacement('ABC', 2)) --> AA AB AC BB BC CC
Raymond Hettingerb8baf632009-01-14 02:20:07 +0000222
223
Georg Brandl116aa622007-08-15 14:28:22 +0000224:class:`deque` objects
225----------------------
226
Georg Brandlc2a4f4f2009-04-10 09:03:43 +0000227.. class:: deque([iterable, [maxlen]])
Georg Brandl116aa622007-08-15 14:28:22 +0000228
229 Returns a new deque object initialized left-to-right (using :meth:`append`) with
230 data from *iterable*. If *iterable* is not specified, the new deque is empty.
231
232 Deques are a generalization of stacks and queues (the name is pronounced "deck"
233 and is short for "double-ended queue"). Deques support thread-safe, memory
234 efficient appends and pops from either side of the deque with approximately the
235 same O(1) performance in either direction.
236
237 Though :class:`list` objects support similar operations, they are optimized for
238 fast fixed-length operations and incur O(n) memory movement costs for
239 ``pop(0)`` and ``insert(0, v)`` operations which change both the size and
240 position of the underlying data representation.
241
Georg Brandl116aa622007-08-15 14:28:22 +0000242
Georg Brandl9afde1c2007-11-01 20:32:30 +0000243 If *maxlen* is not specified or is *None*, deques may grow to an
244 arbitrary length. Otherwise, the deque is bounded to the specified maximum
245 length. Once a bounded length deque is full, when new items are added, a
246 corresponding number of items are discarded from the opposite end. Bounded
247 length deques provide functionality similar to the ``tail`` filter in
248 Unix. They are also useful for tracking transactions and other pools of data
249 where only the most recent activity is of interest.
250
Georg Brandl9afde1c2007-11-01 20:32:30 +0000251
Benjamin Petersone41251e2008-04-25 01:59:09 +0000252 Deque objects support the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000253
Benjamin Petersone41251e2008-04-25 01:59:09 +0000254 .. method:: append(x)
Georg Brandl116aa622007-08-15 14:28:22 +0000255
Benjamin Petersone41251e2008-04-25 01:59:09 +0000256 Add *x* to the right side of the deque.
Georg Brandl116aa622007-08-15 14:28:22 +0000257
258
Benjamin Petersone41251e2008-04-25 01:59:09 +0000259 .. method:: appendleft(x)
Georg Brandl116aa622007-08-15 14:28:22 +0000260
Benjamin Petersone41251e2008-04-25 01:59:09 +0000261 Add *x* to the left side of the deque.
Georg Brandl116aa622007-08-15 14:28:22 +0000262
263
Benjamin Petersone41251e2008-04-25 01:59:09 +0000264 .. method:: clear()
Georg Brandl116aa622007-08-15 14:28:22 +0000265
Benjamin Petersone41251e2008-04-25 01:59:09 +0000266 Remove all elements from the deque leaving it with length 0.
Georg Brandl116aa622007-08-15 14:28:22 +0000267
268
Raymond Hettinger44459de2010-04-03 23:20:46 +0000269 .. method:: count(x)
270
271 Count the number of deque elements equal to *x*.
272
273 .. versionadded:: 3.2
274
Georg Brandl67b21b72010-08-17 15:07:14 +0000275
Benjamin Petersone41251e2008-04-25 01:59:09 +0000276 .. method:: extend(iterable)
Georg Brandl116aa622007-08-15 14:28:22 +0000277
Benjamin Petersone41251e2008-04-25 01:59:09 +0000278 Extend the right side of the deque by appending elements from the iterable
279 argument.
Georg Brandl116aa622007-08-15 14:28:22 +0000280
281
Benjamin Petersone41251e2008-04-25 01:59:09 +0000282 .. method:: extendleft(iterable)
Georg Brandl116aa622007-08-15 14:28:22 +0000283
Benjamin Petersone41251e2008-04-25 01:59:09 +0000284 Extend the left side of the deque by appending elements from *iterable*.
285 Note, the series of left appends results in reversing the order of
286 elements in the iterable argument.
Georg Brandl116aa622007-08-15 14:28:22 +0000287
288
Benjamin Petersone41251e2008-04-25 01:59:09 +0000289 .. method:: pop()
Georg Brandl116aa622007-08-15 14:28:22 +0000290
Benjamin Petersone41251e2008-04-25 01:59:09 +0000291 Remove and return an element from the right side of the deque. If no
292 elements are present, raises an :exc:`IndexError`.
Georg Brandl116aa622007-08-15 14:28:22 +0000293
294
Benjamin Petersone41251e2008-04-25 01:59:09 +0000295 .. method:: popleft()
Georg Brandl116aa622007-08-15 14:28:22 +0000296
Benjamin Petersone41251e2008-04-25 01:59:09 +0000297 Remove and return an element from the left side of the deque. If no
298 elements are present, raises an :exc:`IndexError`.
Georg Brandl116aa622007-08-15 14:28:22 +0000299
300
Benjamin Petersone41251e2008-04-25 01:59:09 +0000301 .. method:: remove(value)
Georg Brandl116aa622007-08-15 14:28:22 +0000302
Benjamin Petersone41251e2008-04-25 01:59:09 +0000303 Removed the first occurrence of *value*. If not found, raises a
304 :exc:`ValueError`.
Georg Brandl116aa622007-08-15 14:28:22 +0000305
Georg Brandl67b21b72010-08-17 15:07:14 +0000306
Raymond Hettingere5fdedb2009-12-10 00:47:21 +0000307 .. method:: reverse()
308
309 Reverse the elements of the deque in-place and then return ``None``.
310
311 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000312
Georg Brandl67b21b72010-08-17 15:07:14 +0000313
Benjamin Petersone41251e2008-04-25 01:59:09 +0000314 .. method:: rotate(n)
Georg Brandl116aa622007-08-15 14:28:22 +0000315
Benjamin Petersone41251e2008-04-25 01:59:09 +0000316 Rotate the deque *n* steps to the right. If *n* is negative, rotate to
317 the left. Rotating one step to the right is equivalent to:
318 ``d.appendleft(d.pop())``.
319
Georg Brandl116aa622007-08-15 14:28:22 +0000320
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +0000321 Deque objects also provide one read-only attribute:
322
323 .. attribute:: maxlen
324
325 Maximum size of a deque or *None* if unbounded.
326
Raymond Hettinger150fb9c2009-03-10 22:48:06 +0000327 .. versionadded:: 3.1
Raymond Hettinger5bb0f0e2009-03-10 12:56:32 +0000328
329
Georg Brandl116aa622007-08-15 14:28:22 +0000330In addition to the above, deques support iteration, pickling, ``len(d)``,
331``reversed(d)``, ``copy.copy(d)``, ``copy.deepcopy(d)``, membership testing with
Benjamin Peterson206e3072008-10-19 14:07:49 +0000332the :keyword:`in` operator, and subscript references such as ``d[-1]``. Indexed
333access is O(1) at both ends but slows to O(n) in the middle. For fast random
334access, use lists instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000335
Christian Heimesfe337bf2008-03-23 21:54:12 +0000336Example:
337
338.. doctest::
Georg Brandl116aa622007-08-15 14:28:22 +0000339
340 >>> from collections import deque
341 >>> d = deque('ghi') # make a new deque with three items
342 >>> for elem in d: # iterate over the deque's elements
Neal Norwitz752abd02008-05-13 04:55:24 +0000343 ... print(elem.upper())
Georg Brandl116aa622007-08-15 14:28:22 +0000344 G
345 H
346 I
347
348 >>> d.append('j') # add a new entry to the right side
349 >>> d.appendleft('f') # add a new entry to the left side
350 >>> d # show the representation of the deque
351 deque(['f', 'g', 'h', 'i', 'j'])
352
353 >>> d.pop() # return and remove the rightmost item
354 'j'
355 >>> d.popleft() # return and remove the leftmost item
356 'f'
357 >>> list(d) # list the contents of the deque
358 ['g', 'h', 'i']
359 >>> d[0] # peek at leftmost item
360 'g'
361 >>> d[-1] # peek at rightmost item
362 'i'
363
364 >>> list(reversed(d)) # list the contents of a deque in reverse
365 ['i', 'h', 'g']
366 >>> 'h' in d # search the deque
367 True
368 >>> d.extend('jkl') # add multiple elements at once
369 >>> d
370 deque(['g', 'h', 'i', 'j', 'k', 'l'])
371 >>> d.rotate(1) # right rotation
372 >>> d
373 deque(['l', 'g', 'h', 'i', 'j', 'k'])
374 >>> d.rotate(-1) # left rotation
375 >>> d
376 deque(['g', 'h', 'i', 'j', 'k', 'l'])
377
378 >>> deque(reversed(d)) # make a new deque in reverse order
379 deque(['l', 'k', 'j', 'i', 'h', 'g'])
380 >>> d.clear() # empty the deque
381 >>> d.pop() # cannot pop from an empty deque
382 Traceback (most recent call last):
383 File "<pyshell#6>", line 1, in -toplevel-
384 d.pop()
385 IndexError: pop from an empty deque
386
387 >>> d.extendleft('abc') # extendleft() reverses the input order
388 >>> d
389 deque(['c', 'b', 'a'])
390
391
Georg Brandl9afde1c2007-11-01 20:32:30 +0000392:class:`deque` Recipes
393^^^^^^^^^^^^^^^^^^^^^^
Georg Brandl116aa622007-08-15 14:28:22 +0000394
395This section shows various approaches to working with deques.
396
Raymond Hettingerd2ee64d2009-03-31 22:52:48 +0000397Bounded length deques provide functionality similar to the ``tail`` filter
398in Unix::
399
400 def tail(filename, n=10):
401 'Return the last n lines of a file'
402 return deque(open(filename), n)
403
404Another approach to using deques is to maintain a sequence of recently
405added elements by appending to the right and popping to the left::
406
407 def moving_average(iterable, n=3):
408 # moving_average([40, 30, 50, 46, 39, 44]) --> 40.0 42.0 45.0 43.0
409 # http://en.wikipedia.org/wiki/Moving_average
410 it = iter(iterable)
Raymond Hettingerd40285a2009-05-22 01:11:26 +0000411 d = deque(itertools.islice(it, n-1))
412 d.appendleft(0)
Raymond Hettingerd2ee64d2009-03-31 22:52:48 +0000413 s = sum(d)
Raymond Hettingerd2ee64d2009-03-31 22:52:48 +0000414 for elem in it:
415 s += elem - d.popleft()
416 d.append(elem)
417 yield s / n
418
Georg Brandl116aa622007-08-15 14:28:22 +0000419The :meth:`rotate` method provides a way to implement :class:`deque` slicing and
Ezio Melotti0639d5a2009-12-19 23:26:38 +0000420deletion. For example, a pure Python implementation of ``del d[n]`` relies on
Georg Brandl116aa622007-08-15 14:28:22 +0000421the :meth:`rotate` method to position elements to be popped::
422
423 def delete_nth(d, n):
424 d.rotate(-n)
425 d.popleft()
426 d.rotate(n)
427
428To implement :class:`deque` slicing, use a similar approach applying
429:meth:`rotate` to bring a target element to the left side of the deque. Remove
430old entries with :meth:`popleft`, add new entries with :meth:`extend`, and then
431reverse the rotation.
Georg Brandl116aa622007-08-15 14:28:22 +0000432With minor variations on that approach, it is easy to implement Forth style
433stack manipulations such as ``dup``, ``drop``, ``swap``, ``over``, ``pick``,
434``rot``, and ``roll``.
435
Georg Brandl116aa622007-08-15 14:28:22 +0000436
437:class:`defaultdict` objects
438----------------------------
439
Georg Brandl116aa622007-08-15 14:28:22 +0000440.. class:: defaultdict([default_factory[, ...]])
441
442 Returns a new dictionary-like object. :class:`defaultdict` is a subclass of the
Georg Brandl22b34312009-07-26 14:54:51 +0000443 built-in :class:`dict` class. It overrides one method and adds one writable
Georg Brandl116aa622007-08-15 14:28:22 +0000444 instance variable. The remaining functionality is the same as for the
445 :class:`dict` class and is not documented here.
446
447 The first argument provides the initial value for the :attr:`default_factory`
448 attribute; it defaults to ``None``. All remaining arguments are treated the same
449 as if they were passed to the :class:`dict` constructor, including keyword
450 arguments.
451
Georg Brandl116aa622007-08-15 14:28:22 +0000452
Benjamin Petersone41251e2008-04-25 01:59:09 +0000453 :class:`defaultdict` objects support the following method in addition to the
454 standard :class:`dict` operations:
Georg Brandl116aa622007-08-15 14:28:22 +0000455
Benjamin Petersond319ad52010-07-18 14:27:02 +0000456 .. method:: __missing__(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000457
Benjamin Peterson5478b472008-09-17 22:25:09 +0000458 If the :attr:`default_factory` attribute is ``None``, this raises a
Benjamin Petersone41251e2008-04-25 01:59:09 +0000459 :exc:`KeyError` exception with the *key* as argument.
Georg Brandl116aa622007-08-15 14:28:22 +0000460
Benjamin Petersone41251e2008-04-25 01:59:09 +0000461 If :attr:`default_factory` is not ``None``, it is called without arguments
462 to provide a default value for the given *key*, this value is inserted in
463 the dictionary for the *key*, and returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000464
Benjamin Petersone41251e2008-04-25 01:59:09 +0000465 If calling :attr:`default_factory` raises an exception this exception is
466 propagated unchanged.
Georg Brandl116aa622007-08-15 14:28:22 +0000467
Benjamin Petersone41251e2008-04-25 01:59:09 +0000468 This method is called by the :meth:`__getitem__` method of the
469 :class:`dict` class when the requested key is not found; whatever it
470 returns or raises is then returned or raised by :meth:`__getitem__`.
Georg Brandl116aa622007-08-15 14:28:22 +0000471
472
Benjamin Petersone41251e2008-04-25 01:59:09 +0000473 :class:`defaultdict` objects support the following instance variable:
Georg Brandl116aa622007-08-15 14:28:22 +0000474
Benjamin Petersone41251e2008-04-25 01:59:09 +0000475
Benjamin Petersond319ad52010-07-18 14:27:02 +0000476 .. attribute:: default_factory
Benjamin Petersone41251e2008-04-25 01:59:09 +0000477
478 This attribute is used by the :meth:`__missing__` method; it is
479 initialized from the first argument to the constructor, if present, or to
480 ``None``, if absent.
Georg Brandl116aa622007-08-15 14:28:22 +0000481
482
Georg Brandl116aa622007-08-15 14:28:22 +0000483:class:`defaultdict` Examples
484^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
485
486Using :class:`list` as the :attr:`default_factory`, it is easy to group a
Christian Heimesfe337bf2008-03-23 21:54:12 +0000487sequence of key-value pairs into a dictionary of lists:
Georg Brandl116aa622007-08-15 14:28:22 +0000488
489 >>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
490 >>> d = defaultdict(list)
491 >>> for k, v in s:
492 ... d[k].append(v)
493 ...
Ezio Melottic53a8942009-09-12 01:52:05 +0000494 >>> list(d.items())
Georg Brandl116aa622007-08-15 14:28:22 +0000495 [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
496
497When each key is encountered for the first time, it is not already in the
498mapping; so an entry is automatically created using the :attr:`default_factory`
499function which returns an empty :class:`list`. The :meth:`list.append`
500operation then attaches the value to the new list. When keys are encountered
501again, the look-up proceeds normally (returning the list for that key) and the
502:meth:`list.append` operation adds another value to the list. This technique is
Christian Heimesfe337bf2008-03-23 21:54:12 +0000503simpler and faster than an equivalent technique using :meth:`dict.setdefault`:
Georg Brandl116aa622007-08-15 14:28:22 +0000504
505 >>> d = {}
506 >>> for k, v in s:
507 ... d.setdefault(k, []).append(v)
508 ...
Ezio Melottic53a8942009-09-12 01:52:05 +0000509 >>> list(d.items())
Georg Brandl116aa622007-08-15 14:28:22 +0000510 [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
511
512Setting the :attr:`default_factory` to :class:`int` makes the
513:class:`defaultdict` useful for counting (like a bag or multiset in other
Christian Heimesfe337bf2008-03-23 21:54:12 +0000514languages):
Georg Brandl116aa622007-08-15 14:28:22 +0000515
516 >>> s = 'mississippi'
517 >>> d = defaultdict(int)
518 >>> for k in s:
519 ... d[k] += 1
520 ...
Ezio Melottic53a8942009-09-12 01:52:05 +0000521 >>> list(d.items())
Georg Brandl116aa622007-08-15 14:28:22 +0000522 [('i', 4), ('p', 2), ('s', 4), ('m', 1)]
523
524When a letter is first encountered, it is missing from the mapping, so the
525:attr:`default_factory` function calls :func:`int` to supply a default count of
526zero. The increment operation then builds up the count for each letter.
527
528The function :func:`int` which always returns zero is just a special case of
529constant functions. A faster and more flexible way to create constant functions
530is to use a lambda function which can supply any constant value (not just
Christian Heimesfe337bf2008-03-23 21:54:12 +0000531zero):
Georg Brandl116aa622007-08-15 14:28:22 +0000532
533 >>> def constant_factory(value):
534 ... return lambda: value
535 >>> d = defaultdict(constant_factory('<missing>'))
536 >>> d.update(name='John', action='ran')
537 >>> '%(name)s %(action)s to %(object)s' % d
538 'John ran to <missing>'
539
540Setting the :attr:`default_factory` to :class:`set` makes the
Christian Heimesfe337bf2008-03-23 21:54:12 +0000541:class:`defaultdict` useful for building a dictionary of sets:
Georg Brandl116aa622007-08-15 14:28:22 +0000542
543 >>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
544 >>> d = defaultdict(set)
545 >>> for k, v in s:
546 ... d[k].add(v)
547 ...
Ezio Melottic53a8942009-09-12 01:52:05 +0000548 >>> list(d.items())
Georg Brandl116aa622007-08-15 14:28:22 +0000549 [('blue', set([2, 4])), ('red', set([1, 3]))]
550
551
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000552:func:`namedtuple` Factory Function for Tuples with Named Fields
Christian Heimes790c8232008-01-07 21:14:23 +0000553----------------------------------------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000554
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000555Named tuples assign meaning to each position in a tuple and allow for more readable,
556self-documenting code. They can be used wherever regular tuples are used, and
557they add the ability to access fields by name instead of position index.
Georg Brandl116aa622007-08-15 14:28:22 +0000558
Georg Brandlc2a4f4f2009-04-10 09:03:43 +0000559.. function:: namedtuple(typename, field_names, verbose=False, rename=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000560
561 Returns a new tuple subclass named *typename*. The new subclass is used to
Christian Heimesc3f30c42008-02-22 16:37:40 +0000562 create tuple-like objects that have fields accessible by attribute lookup as
Georg Brandl116aa622007-08-15 14:28:22 +0000563 well as being indexable and iterable. Instances of the subclass also have a
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000564 helpful docstring (with typename and field_names) and a helpful :meth:`__repr__`
Georg Brandl116aa622007-08-15 14:28:22 +0000565 method which lists the tuple contents in a ``name=value`` format.
566
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000567 The *field_names* are a single string with each fieldname separated by whitespace
568 and/or commas, for example ``'x y'`` or ``'x, y'``. Alternatively, *field_names*
Christian Heimes25bb7832008-01-11 16:17:00 +0000569 can be a sequence of strings such as ``['x', 'y']``.
Georg Brandl9afde1c2007-11-01 20:32:30 +0000570
571 Any valid Python identifier may be used for a fieldname except for names
Christian Heimes0449f632007-12-15 01:27:15 +0000572 starting with an underscore. Valid identifiers consist of letters, digits,
573 and underscores but do not start with a digit or underscore and cannot be
Georg Brandlf6945182008-02-01 11:56:49 +0000574 a :mod:`keyword` such as *class*, *for*, *return*, *global*, *pass*,
Georg Brandl9afde1c2007-11-01 20:32:30 +0000575 or *raise*.
Georg Brandl116aa622007-08-15 14:28:22 +0000576
Benjamin Petersona86f2c02009-02-10 02:41:10 +0000577 If *rename* is true, invalid fieldnames are automatically replaced
578 with positional names. For example, ``['abc', 'def', 'ghi', 'abc']`` is
Raymond Hettinger85737b82009-04-02 22:37:59 +0000579 converted to ``['abc', '_1', 'ghi', '_3']``, eliminating the keyword
Benjamin Petersona86f2c02009-02-10 02:41:10 +0000580 ``def`` and the duplicate fieldname ``abc``.
581
Christian Heimes25bb7832008-01-11 16:17:00 +0000582 If *verbose* is true, the class definition is printed just before being built.
Georg Brandl116aa622007-08-15 14:28:22 +0000583
Georg Brandl9afde1c2007-11-01 20:32:30 +0000584 Named tuple instances do not have per-instance dictionaries, so they are
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000585 lightweight and require no more memory than regular tuples.
Georg Brandl116aa622007-08-15 14:28:22 +0000586
Raymond Hettingerb62ad242009-03-02 22:16:43 +0000587 .. versionchanged:: 3.1
Georg Brandl67b21b72010-08-17 15:07:14 +0000588 Added support for *rename*.
Benjamin Petersona86f2c02009-02-10 02:41:10 +0000589
Christian Heimesfe337bf2008-03-23 21:54:12 +0000590
591.. doctest::
592 :options: +NORMALIZE_WHITESPACE
Georg Brandl116aa622007-08-15 14:28:22 +0000593
Raymond Hettinger0ef956f2010-11-21 23:23:29 +0000594 >>> # Basic example
Raymond Hettinger15aded82011-03-15 17:25:51 -0700595 >>> Point = namedtuple('Point', ['x', 'y'])
Raymond Hettinger0ef956f2010-11-21 23:23:29 +0000596 >>> p = Point(x=10, y=11)
597
598 >>> # Example using the verbose option to print the class definition
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000599 >>> Point = namedtuple('Point', 'x y', verbose=True)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000600 class Point(tuple):
601 'Point(x, y)'
Christian Heimesfe337bf2008-03-23 21:54:12 +0000602 <BLANKLINE>
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000603 __slots__ = ()
Christian Heimesfe337bf2008-03-23 21:54:12 +0000604 <BLANKLINE>
Christian Heimesfaf2f632008-01-06 16:59:19 +0000605 _fields = ('x', 'y')
Christian Heimesfe337bf2008-03-23 21:54:12 +0000606 <BLANKLINE>
Raymond Hettinger089ba7f2009-05-27 00:38:24 +0000607 def __new__(_cls, x, y):
Raymond Hettinger7b0d3c62010-04-02 18:54:02 +0000608 'Create a new instance of Point(x, y)'
Raymond Hettinger089ba7f2009-05-27 00:38:24 +0000609 return _tuple.__new__(_cls, (x, y))
Christian Heimesfe337bf2008-03-23 21:54:12 +0000610 <BLANKLINE>
Christian Heimesfaf2f632008-01-06 16:59:19 +0000611 @classmethod
Christian Heimesfe337bf2008-03-23 21:54:12 +0000612 def _make(cls, iterable, new=tuple.__new__, len=len):
Christian Heimesfaf2f632008-01-06 16:59:19 +0000613 'Make a new Point object from a sequence or iterable'
Christian Heimesfe337bf2008-03-23 21:54:12 +0000614 result = new(cls, iterable)
Christian Heimesfaf2f632008-01-06 16:59:19 +0000615 if len(result) != 2:
616 raise TypeError('Expected 2 arguments, got %d' % len(result))
617 return result
Christian Heimesfe337bf2008-03-23 21:54:12 +0000618 <BLANKLINE>
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000619 def __repr__(self):
Raymond Hettinger7b0d3c62010-04-02 18:54:02 +0000620 'Return a nicely formatted representation string'
Raymond Hettingerd331ce92010-08-08 01:13:42 +0000621 return self.__class__.__name__ + '(x=%r, y=%r)' % self
Christian Heimesfe337bf2008-03-23 21:54:12 +0000622 <BLANKLINE>
Raymond Hettingera4f52b12009-03-02 22:28:31 +0000623 def _asdict(self):
624 'Return a new OrderedDict which maps field names to their values'
625 return OrderedDict(zip(self._fields, self))
Christian Heimesfe337bf2008-03-23 21:54:12 +0000626 <BLANKLINE>
Raymond Hettinger3d890572011-06-02 23:40:24 -0700627 __dict__ = property(_asdict)
628 <BLANKLINE>
Raymond Hettinger089ba7f2009-05-27 00:38:24 +0000629 def _replace(_self, **kwds):
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000630 'Return a new Point object replacing specified fields with new values'
Raymond Hettinger089ba7f2009-05-27 00:38:24 +0000631 result = _self._make(map(kwds.pop, ('x', 'y'), _self))
Christian Heimesfaf2f632008-01-06 16:59:19 +0000632 if kwds:
Ezio Melotti8f7649e2009-09-13 04:48:45 +0000633 raise ValueError('Got unexpected field names: %r' % list(kwds.keys()))
Christian Heimesfaf2f632008-01-06 16:59:19 +0000634 return result
Georg Brandl48310cd2009-01-03 21:18:54 +0000635 <BLANKLINE>
636 def __getnewargs__(self):
Raymond Hettinger7b0d3c62010-04-02 18:54:02 +0000637 'Return self as a plain tuple. Used by copy and pickle.'
Benjamin Peterson41181742008-07-02 20:22:54 +0000638 return tuple(self)
Christian Heimesfe337bf2008-03-23 21:54:12 +0000639 <BLANKLINE>
Raymond Hettinger7b0d3c62010-04-02 18:54:02 +0000640 x = _property(_itemgetter(0), doc='Alias for field number 0')
641 y = _property(_itemgetter(1), doc='Alias for field number 1')
Georg Brandl116aa622007-08-15 14:28:22 +0000642
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000643 >>> p = Point(11, y=22) # instantiate with positional or keyword arguments
Christian Heimes99170a52007-12-19 02:07:34 +0000644 >>> p[0] + p[1] # indexable like the plain tuple (11, 22)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000645 33
646 >>> x, y = p # unpack like a regular tuple
647 >>> x, y
648 (11, 22)
Christian Heimesc3f30c42008-02-22 16:37:40 +0000649 >>> p.x + p.y # fields also accessible by name
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000650 33
651 >>> p # readable __repr__ with a name=value style
652 Point(x=11, y=22)
Georg Brandl116aa622007-08-15 14:28:22 +0000653
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000654Named tuples are especially useful for assigning field names to result tuples returned
655by the :mod:`csv` or :mod:`sqlite3` modules::
656
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000657 EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade')
Georg Brandl9afde1c2007-11-01 20:32:30 +0000658
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000659 import csv
Christian Heimesfaf2f632008-01-06 16:59:19 +0000660 for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))):
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000661 print(emp.name, emp.title)
662
Georg Brandl9afde1c2007-11-01 20:32:30 +0000663 import sqlite3
664 conn = sqlite3.connect('/companydata')
665 cursor = conn.cursor()
666 cursor.execute('SELECT name, age, title, department, paygrade FROM employees')
Christian Heimesfaf2f632008-01-06 16:59:19 +0000667 for emp in map(EmployeeRecord._make, cursor.fetchall()):
Christian Heimes00412232008-01-10 16:02:19 +0000668 print(emp.name, emp.title)
Georg Brandl9afde1c2007-11-01 20:32:30 +0000669
Christian Heimes99170a52007-12-19 02:07:34 +0000670In addition to the methods inherited from tuples, named tuples support
Christian Heimes2380ac72008-01-09 00:17:24 +0000671three additional methods and one attribute. To prevent conflicts with
672field names, the method and attribute names start with an underscore.
Christian Heimes99170a52007-12-19 02:07:34 +0000673
Benjamin Peterson0b9fb802010-07-18 14:23:36 +0000674.. classmethod:: somenamedtuple._make(iterable)
Christian Heimes99170a52007-12-19 02:07:34 +0000675
Christian Heimesfaf2f632008-01-06 16:59:19 +0000676 Class method that makes a new instance from an existing sequence or iterable.
Christian Heimes99170a52007-12-19 02:07:34 +0000677
Christian Heimesfe337bf2008-03-23 21:54:12 +0000678.. doctest::
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000679
Christian Heimesfaf2f632008-01-06 16:59:19 +0000680 >>> t = [11, 22]
681 >>> Point._make(t)
682 Point(x=11, y=22)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000683
Christian Heimes790c8232008-01-07 21:14:23 +0000684.. method:: somenamedtuple._asdict()
Georg Brandl9afde1c2007-11-01 20:32:30 +0000685
Raymond Hettingera4f52b12009-03-02 22:28:31 +0000686 Return a new :class:`OrderedDict` which maps field names to their corresponding
687 values::
Georg Brandl9afde1c2007-11-01 20:32:30 +0000688
Christian Heimes0449f632007-12-15 01:27:15 +0000689 >>> p._asdict()
Raymond Hettingera4f52b12009-03-02 22:28:31 +0000690 OrderedDict([('x', 11), ('y', 22)])
691
Raymond Hettingera88e4da2009-03-03 05:12:27 +0000692 .. versionchanged:: 3.1
Raymond Hettingera4f52b12009-03-02 22:28:31 +0000693 Returns an :class:`OrderedDict` instead of a regular :class:`dict`.
Christian Heimesfe337bf2008-03-23 21:54:12 +0000694
Christian Heimes790c8232008-01-07 21:14:23 +0000695.. method:: somenamedtuple._replace(kwargs)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000696
Christian Heimesfe337bf2008-03-23 21:54:12 +0000697 Return a new instance of the named tuple replacing specified fields with new
698 values:
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000699
700::
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000701
702 >>> p = Point(x=11, y=22)
Christian Heimes0449f632007-12-15 01:27:15 +0000703 >>> p._replace(x=33)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000704 Point(x=33, y=22)
705
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000706 >>> for partnum, record in inventory.items():
Christian Heimes454f37b2008-01-10 00:10:02 +0000707 ... inventory[partnum] = record._replace(price=newprices[partnum], timestamp=time.now())
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000708
Christian Heimes790c8232008-01-07 21:14:23 +0000709.. attribute:: somenamedtuple._fields
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000710
Christian Heimes2380ac72008-01-09 00:17:24 +0000711 Tuple of strings listing the field names. Useful for introspection
Georg Brandl9afde1c2007-11-01 20:32:30 +0000712 and for creating new named tuple types from existing named tuples.
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000713
Christian Heimesfe337bf2008-03-23 21:54:12 +0000714.. doctest::
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000715
Christian Heimes0449f632007-12-15 01:27:15 +0000716 >>> p._fields # view the field names
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000717 ('x', 'y')
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000718
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000719 >>> Color = namedtuple('Color', 'red green blue')
Christian Heimes0449f632007-12-15 01:27:15 +0000720 >>> Pixel = namedtuple('Pixel', Point._fields + Color._fields)
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000721 >>> Pixel(11, 22, 128, 255, 0)
Christian Heimes454f37b2008-01-10 00:10:02 +0000722 Pixel(x=11, y=22, red=128, green=255, blue=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000723
Christian Heimes0449f632007-12-15 01:27:15 +0000724To retrieve a field whose name is stored in a string, use the :func:`getattr`
Christian Heimesfe337bf2008-03-23 21:54:12 +0000725function:
Christian Heimes0449f632007-12-15 01:27:15 +0000726
727 >>> getattr(p, 'x')
728 11
729
Raymond Hettinger651453a2009-02-11 00:20:02 +0000730To convert a dictionary to a named tuple, use the double-star-operator
731(as described in :ref:`tut-unpacking-arguments`):
Christian Heimes99170a52007-12-19 02:07:34 +0000732
733 >>> d = {'x': 11, 'y': 22}
734 >>> Point(**d)
735 Point(x=11, y=22)
736
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000737Since a named tuple is a regular Python class, it is easy to add or change
Christian Heimes043d6f62008-01-07 17:19:16 +0000738functionality with a subclass. Here is how to add a calculated field and
Christian Heimesfe337bf2008-03-23 21:54:12 +0000739a fixed-width print format:
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000740
Christian Heimes043d6f62008-01-07 17:19:16 +0000741 >>> class Point(namedtuple('Point', 'x y')):
Raymond Hettinger15aded82011-03-15 17:25:51 -0700742 __slots__ = ()
743 @property
744 def hypot(self):
745 return (self.x ** 2 + self.y ** 2) ** 0.5
746 def __str__(self):
747 return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000748
Georg Brandl0df79792008-10-04 18:33:26 +0000749 >>> for p in Point(3, 4), Point(14, 5/7):
Raymond Hettinger15aded82011-03-15 17:25:51 -0700750 print(p)
Christian Heimes25bb7832008-01-11 16:17:00 +0000751 Point: x= 3.000 y= 4.000 hypot= 5.000
752 Point: x=14.000 y= 0.714 hypot=14.018
Christian Heimes043d6f62008-01-07 17:19:16 +0000753
Georg Brandlaf5c2382009-12-28 08:02:38 +0000754The subclass shown above sets ``__slots__`` to an empty tuple. This helps
Christian Heimes679db4a2008-01-18 09:56:22 +0000755keep memory requirements low by preventing the creation of instance dictionaries.
756
Christian Heimes2380ac72008-01-09 00:17:24 +0000757
758Subclassing is not useful for adding new, stored fields. Instead, simply
Christian Heimesfe337bf2008-03-23 21:54:12 +0000759create a new named tuple type from the :attr:`_fields` attribute:
Christian Heimes2380ac72008-01-09 00:17:24 +0000760
Christian Heimes25bb7832008-01-11 16:17:00 +0000761 >>> Point3D = namedtuple('Point3D', Point._fields + ('z',))
Christian Heimes2380ac72008-01-09 00:17:24 +0000762
763Default values can be implemented by using :meth:`_replace` to
Christian Heimesfe337bf2008-03-23 21:54:12 +0000764customize a prototype instance:
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000765
766 >>> Account = namedtuple('Account', 'owner balance transaction_count')
Christian Heimes587c2bf2008-01-19 16:21:02 +0000767 >>> default_account = Account('<owner name>', 0.0, 0)
768 >>> johns_account = default_account._replace(owner='John')
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000769
Christian Heimese4ca8152008-05-08 17:18:53 +0000770Enumerated constants can be implemented with named tuples, but it is simpler
771and more efficient to use a simple class declaration:
772
773 >>> Status = namedtuple('Status', 'open pending closed')._make(range(3))
774 >>> Status.open, Status.pending, Status.closed
775 (0, 1, 2)
776 >>> class Status:
Raymond Hettinger15aded82011-03-15 17:25:51 -0700777 open, pending, closed = range(3)
Christian Heimese4ca8152008-05-08 17:18:53 +0000778
Raymond Hettinger651453a2009-02-11 00:20:02 +0000779.. seealso::
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000780
Raymond Hettinger6c94e6f2011-03-31 15:46:06 -0700781 * `Named tuple recipe <http://code.activestate.com/recipes/500261/>`_
782 adapted for Python 2.4.
783
784 * `Recipe for named tuple abstract base class with a metaclass mix-in
785 <http://code.activestate.com/recipes/577629-namedtupleabc-abstract-base-class-mix-in-for-named/>`_
786 by Jan Kaliszewski. Besides providing an :term:`abstract base class` for
787 named tuples, it also supports an alternate :term:`metaclass`-based
788 constructor that is convenient for use cases where named tuples are being
789 subclassed.
Raymond Hettingere4c96ad2008-02-06 01:23:58 +0000790
791
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000792:class:`OrderedDict` objects
793----------------------------
794
795Ordered dictionaries are just like regular dictionaries but they remember the
796order that items were inserted. When iterating over an ordered dictionary,
797the items are returned in the order their keys were first added.
798
799.. class:: OrderedDict([items])
800
801 Return an instance of a dict subclass, supporting the usual :class:`dict`
802 methods. An *OrderedDict* is a dict that remembers the order that keys
803 were first inserted. If a new entry overwrites an existing entry, the
804 original insertion position is left unchanged. Deleting an entry and
805 reinserting it will move it to the end.
806
Benjamin Petersond45bf582009-03-02 21:44:54 +0000807 .. versionadded:: 3.1
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000808
Benjamin Petersond319ad52010-07-18 14:27:02 +0000809 .. method:: popitem(last=True)
Raymond Hettingerdc879f02009-03-19 20:30:56 +0000810
Benjamin Petersond319ad52010-07-18 14:27:02 +0000811 The :meth:`popitem` method for ordered dictionaries returns and removes a
812 (key, value) pair. The pairs are returned in LIFO order if *last* is true
813 or FIFO order if false.
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000814
Raymond Hettingerf45abc92010-09-06 21:26:09 +0000815 .. method:: move_to_end(key, last=True)
816
817 Move an existing *key* to either end of an ordered dictionary. The item
818 is moved to the right end if *last* is true (the default) or to the
819 beginning if *last* is false. Raises :exc:`KeyError` if the *key* does
820 not exist::
821
822 >>> d = OrderedDict.fromkeys('abcde')
823 >>> d.move_to_end('b')
824 >>> ''.join(d.keys)
825 'acdeb'
Éric Araujo1cb25aa2010-11-06 07:03:07 +0000826 >>> d.move_to_end('b', last=False)
Raymond Hettingerf45abc92010-09-06 21:26:09 +0000827 >>> ''.join(d.keys)
828 'bacde'
829
830 .. versionadded:: 3.2
831
Raymond Hettingere9091502009-05-19 17:40:07 +0000832In addition to the usual mapping methods, ordered dictionaries also support
833reverse iteration using :func:`reversed`.
834
Raymond Hettinger2d32f632009-03-02 21:24:57 +0000835Equality tests between :class:`OrderedDict` objects are order-sensitive
836and are implemented as ``list(od1.items())==list(od2.items())``.
837Equality tests between :class:`OrderedDict` objects and other
838:class:`Mapping` objects are order-insensitive like regular dictionaries.
839This allows :class:`OrderedDict` objects to be substituted anywhere a
840regular dictionary is used.
841
Raymond Hettinger36180782009-04-09 22:34:23 +0000842The :class:`OrderedDict` constructor and :meth:`update` method both accept
843keyword arguments, but their order is lost because Python's function call
844semantics pass-in keyword arguments using a regular unordered dictionary.
845
Raymond Hettingerdc879f02009-03-19 20:30:56 +0000846.. seealso::
847
848 `Equivalent OrderedDict recipe <http://code.activestate.com/recipes/576693/>`_
849 that runs on Python 2.4 or later.
850
Raymond Hettinger7bba6832011-04-15 17:43:19 -0700851:class:`OrderedDict` Examples and Recipes
852^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
853
Raymond Hettinger0e312012009-11-10 18:35:46 +0000854Since an ordered dictionary remembers its insertion order, it can be used
855in conjuction with sorting to make a sorted dictionary::
856
857 >>> # regular unsorted dictionary
858 >>> d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}
859
860 >>> # dictionary sorted by key
861 >>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))
862 OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])
863
864 >>> # dictionary sorted by value
865 >>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))
866 OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])
867
868 >>> # dictionary sorted by length of the key string
869 >>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))
870 OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])
871
872The new sorted dictionaries maintain their sort order when entries
873are deleted. But when new keys are added, the keys are appended
874to the end and the sort is not maintained.
875
Raymond Hettinger4821ef82010-07-31 10:14:41 +0000876It is also straight-forward to create an ordered dictionary variant
877that the remembers the order the keys were *last* inserted.
878If a new entry overwrites an existing entry, the
879original insertion position is changed and moved to the end::
880
881 class LastUpdatedOrderedDict(OrderedDict):
Georg Brandl77570e22010-12-18 16:21:58 +0000882 'Store items in the order the keys were last added'
Raymond Hettinger7bba6832011-04-15 17:43:19 -0700883
Raymond Hettinger4821ef82010-07-31 10:14:41 +0000884 def __setitem__(self, key, value):
885 if key in self:
886 del self[key]
887 OrderedDict.__setitem__(self, key, value)
888
Raymond Hettinger7bba6832011-04-15 17:43:19 -0700889An ordered dictionary can combined with the :class:`Counter` class
890so that the counter remembers the order elements are first encountered::
891
892 class OrderedCounter(Counter, OrderedDict):
893 'Counter that remembers the order elements are first encountered'
894
Raymond Hettinger7bba6832011-04-15 17:43:19 -0700895 def __repr__(self):
896 return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))
897
898 def __reduce__(self):
899 return self.__class__, (OrderedDict(self),)
900
Raymond Hettingere4c96ad2008-02-06 01:23:58 +0000901
902:class:`UserDict` objects
Mark Summerfield8f2d0062008-02-06 13:30:44 +0000903-------------------------
Raymond Hettingere4c96ad2008-02-06 01:23:58 +0000904
Georg Brandl48310cd2009-01-03 21:18:54 +0000905The class, :class:`UserDict` acts as a wrapper around dictionary objects.
906The need for this class has been partially supplanted by the ability to
Raymond Hettingere4c96ad2008-02-06 01:23:58 +0000907subclass directly from :class:`dict`; however, this class can be easier
908to work with because the underlying dictionary is accessible as an
909attribute.
910
911.. class:: UserDict([initialdata])
912
913 Class that simulates a dictionary. The instance's contents are kept in a
914 regular dictionary, which is accessible via the :attr:`data` attribute of
915 :class:`UserDict` instances. If *initialdata* is provided, :attr:`data` is
916 initialized with its contents; note that a reference to *initialdata* will not
917 be kept, allowing it be used for other purposes.
918
Benjamin Petersond319ad52010-07-18 14:27:02 +0000919 In addition to supporting the methods and operations of mappings,
920 :class:`UserDict` instances provide the following attribute:
Raymond Hettingere4c96ad2008-02-06 01:23:58 +0000921
Benjamin Petersond319ad52010-07-18 14:27:02 +0000922 .. attribute:: data
Raymond Hettingere4c96ad2008-02-06 01:23:58 +0000923
Benjamin Petersond319ad52010-07-18 14:27:02 +0000924 A real dictionary used to store the contents of the :class:`UserDict`
925 class.
Raymond Hettinger53dbe392008-02-12 20:03:09 +0000926
927
928
929:class:`UserList` objects
930-------------------------
931
932This class acts as a wrapper around list objects. It is a useful base class
Georg Brandl48310cd2009-01-03 21:18:54 +0000933for your own list-like classes which can inherit from them and override
Raymond Hettinger53dbe392008-02-12 20:03:09 +0000934existing methods or add new ones. In this way, one can add new behaviors to
935lists.
936
Georg Brandl48310cd2009-01-03 21:18:54 +0000937The need for this class has been partially supplanted by the ability to
Raymond Hettinger53dbe392008-02-12 20:03:09 +0000938subclass directly from :class:`list`; however, this class can be easier
939to work with because the underlying list is accessible as an attribute.
940
941.. class:: UserList([list])
942
943 Class that simulates a list. The instance's contents are kept in a regular
944 list, which is accessible via the :attr:`data` attribute of :class:`UserList`
945 instances. The instance's contents are initially set to a copy of *list*,
946 defaulting to the empty list ``[]``. *list* can be any iterable, for
947 example a real Python list or a :class:`UserList` object.
948
Benjamin Petersond319ad52010-07-18 14:27:02 +0000949 In addition to supporting the methods and operations of mutable sequences,
950 :class:`UserList` instances provide the following attribute:
Raymond Hettinger53dbe392008-02-12 20:03:09 +0000951
Benjamin Petersond319ad52010-07-18 14:27:02 +0000952 .. attribute:: data
Raymond Hettinger53dbe392008-02-12 20:03:09 +0000953
Benjamin Petersond319ad52010-07-18 14:27:02 +0000954 A real :class:`list` object used to store the contents of the
955 :class:`UserList` class.
Raymond Hettinger53dbe392008-02-12 20:03:09 +0000956
957**Subclassing requirements:** Subclasses of :class:`UserList` are expect to
958offer a constructor which can be called with either no arguments or one
959argument. List operations which return a new sequence attempt to create an
960instance of the actual implementation class. To do so, it assumes that the
961constructor can be called with a single parameter, which is a sequence object
962used as a data source.
963
964If a derived class does not wish to comply with this requirement, all of the
965special methods supported by this class will need to be overridden; please
966consult the sources for information about the methods which need to be provided
967in that case.
Raymond Hettingerb3a65f82008-02-21 22:11:37 +0000968
969:class:`UserString` objects
Christian Heimesc3f30c42008-02-22 16:37:40 +0000970---------------------------
Raymond Hettingerb3a65f82008-02-21 22:11:37 +0000971
Georg Brandl48310cd2009-01-03 21:18:54 +0000972The class, :class:`UserString` acts as a wrapper around string objects.
973The need for this class has been partially supplanted by the ability to
Raymond Hettingerb3a65f82008-02-21 22:11:37 +0000974subclass directly from :class:`str`; however, this class can be easier
975to work with because the underlying string is accessible as an
976attribute.
977
978.. class:: UserString([sequence])
979
980 Class that simulates a string or a Unicode string object. The instance's
Georg Brandl48310cd2009-01-03 21:18:54 +0000981 content is kept in a regular string object, which is accessible via the
982 :attr:`data` attribute of :class:`UserString` instances. The instance's
Raymond Hettingerb3a65f82008-02-21 22:11:37 +0000983 contents are initially set to a copy of *sequence*. The *sequence* can
984 be an instance of :class:`bytes`, :class:`str`, :class:`UserString` (or a
985 subclass) or an arbitrary sequence which can be converted into a string using
986 the built-in :func:`str` function.
Raymond Hettingera6b76ba2010-08-08 00:29:08 +0000987
Éric Araujofa088db2011-06-04 18:42:38 +0200988.. _collections-abstract-base-classes:
Raymond Hettingera6b76ba2010-08-08 00:29:08 +0000989
990ABCs - abstract base classes
991----------------------------
992
Ezio Melotti9b2e67c2011-03-28 13:50:41 +0300993The collections module offers the following :term:`ABCs <abstract base class>`:
Raymond Hettingera6b76ba2010-08-08 00:29:08 +0000994
995========================= ===================== ====================== ====================================================
Ezio Melotti9b2e67c2011-03-28 13:50:41 +0300996ABC Inherits from Abstract Methods Mixin Methods
Raymond Hettingera6b76ba2010-08-08 00:29:08 +0000997========================= ===================== ====================== ====================================================
998:class:`Container` ``__contains__``
999:class:`Hashable` ``__hash__``
1000:class:`Iterable` ``__iter__``
1001:class:`Iterator` :class:`Iterable` ``__next__`` ``__iter__``
1002:class:`Sized` ``__len__``
1003:class:`Callable` ``__call__``
1004
Georg Brandle951e912011-02-03 07:08:25 +00001005:class:`Sequence` :class:`Sized`, ``__getitem__`` ``__contains__``, ``__iter__``, ``__reversed__``,
Raymond Hettingera6b76ba2010-08-08 00:29:08 +00001006 :class:`Iterable`, ``index``, and ``count``
1007 :class:`Container`
1008
Ezio Melotti9b2e67c2011-03-28 13:50:41 +03001009:class:`MutableSequence` :class:`Sequence` ``__setitem__``, Inherited :class:`Sequence` methods and
Raymond Hettingera6b76ba2010-08-08 00:29:08 +00001010 ``__delitem__``, ``append``, ``reverse``, ``extend``, ``pop``,
Ezio Melotti9b2e67c2011-03-28 13:50:41 +03001011 ``insert`` ``remove``, and ``__iadd__``
Raymond Hettingera6b76ba2010-08-08 00:29:08 +00001012
1013:class:`Set` :class:`Sized`, ``__le__``, ``__lt__``, ``__eq__``, ``__ne__``,
Georg Brandle951e912011-02-03 07:08:25 +00001014 :class:`Iterable`, ``__gt__``, ``__ge__``, ``__and__``, ``__or__``,
Raymond Hettingera6b76ba2010-08-08 00:29:08 +00001015 :class:`Container` ``__sub__``, ``__xor__``, and ``isdisjoint``
1016
Ezio Melotti9b2e67c2011-03-28 13:50:41 +03001017:class:`MutableSet` :class:`Set` ``add``, Inherited :class:`Set` methods and
Raymond Hettingera6b76ba2010-08-08 00:29:08 +00001018 ``discard`` ``clear``, ``pop``, ``remove``, ``__ior__``,
1019 ``__iand__``, ``__ixor__``, and ``__isub__``
1020
1021:class:`Mapping` :class:`Sized`, ``__getitem__`` ``__contains__``, ``keys``, ``items``, ``values``,
1022 :class:`Iterable`, ``get``, ``__eq__``, and ``__ne__``
1023 :class:`Container`
1024
Ezio Melotti9b2e67c2011-03-28 13:50:41 +03001025:class:`MutableMapping` :class:`Mapping` ``__setitem__``, Inherited :class:`Mapping` methods and
Raymond Hettingera6b76ba2010-08-08 00:29:08 +00001026 ``__delitem__`` ``pop``, ``popitem``, ``clear``, ``update``,
1027 and ``setdefault``
1028
1029
1030:class:`MappingView` :class:`Sized` ``__len__``
Raymond Hettingera6b76ba2010-08-08 00:29:08 +00001031:class:`ItemsView` :class:`MappingView`, ``__contains__``,
1032 :class:`Set` ``__iter__``
Ezio Melotti9b2e67c2011-03-28 13:50:41 +03001033:class:`KeysView` :class:`MappingView`, ``__contains__``,
1034 :class:`Set` ``__iter__``
Raymond Hettingera6b76ba2010-08-08 00:29:08 +00001035:class:`ValuesView` :class:`MappingView` ``__contains__``, ``__iter__``
1036========================= ===================== ====================== ====================================================
1037
Ezio Melotti9b2e67c2011-03-28 13:50:41 +03001038
1039.. class:: Container
1040 Hashable
1041 Sized
1042 Callable
1043
1044 ABCs for classes that provide respectively the methods :meth:`__contains__`,
1045 :meth:`__hash__`, :meth:`__len__`, and :meth:`__call__`.
1046
1047.. class:: Iterable
1048
1049 ABC for classes that provide the :meth:`__iter__` method.
1050 See also the definition of :term:`iterable`.
1051
1052.. class:: Iterator
1053
1054 ABC for classes that provide the :meth:`__iter__` and :meth:`next` methods.
1055 See also the definition of :term:`iterator`.
1056
1057.. class:: Sequence
1058 MutableSequence
1059
1060 ABCs for read-only and mutable :term:`sequences <sequence>`.
1061
1062.. class:: Set
1063 MutableSet
1064
1065 ABCs for read-only and mutable sets.
1066
1067.. class:: Mapping
1068 MutableMapping
1069
1070 ABCs for read-only and mutable :term:`mappings <mapping>`.
1071
1072.. class:: MappingView
1073 ItemsView
1074 KeysView
1075 ValuesView
1076
1077 ABCs for mapping, items, keys, and values :term:`views <view>`.
1078
1079
Raymond Hettingera6b76ba2010-08-08 00:29:08 +00001080These ABCs allow us to ask classes or instances if they provide
1081particular functionality, for example::
1082
1083 size = None
1084 if isinstance(myvar, collections.Sized):
1085 size = len(myvar)
1086
1087Several of the ABCs are also useful as mixins that make it easier to develop
1088classes supporting container APIs. For example, to write a class supporting
1089the full :class:`Set` API, it only necessary to supply the three underlying
1090abstract methods: :meth:`__contains__`, :meth:`__iter__`, and :meth:`__len__`.
1091The ABC supplies the remaining methods such as :meth:`__and__` and
1092:meth:`isdisjoint` ::
1093
1094 class ListBasedSet(collections.Set):
1095 ''' Alternate set implementation favoring space over speed
1096 and not requiring the set elements to be hashable. '''
1097 def __init__(self, iterable):
1098 self.elements = lst = []
1099 for value in iterable:
1100 if value not in lst:
1101 lst.append(value)
1102 def __iter__(self):
1103 return iter(self.elements)
1104 def __contains__(self, value):
1105 return value in self.elements
1106 def __len__(self):
1107 return len(self.elements)
1108
1109 s1 = ListBasedSet('abcdef')
1110 s2 = ListBasedSet('defghi')
1111 overlap = s1 & s2 # The __and__() method is supported automatically
1112
1113Notes on using :class:`Set` and :class:`MutableSet` as a mixin:
1114
1115(1)
1116 Since some set operations create new sets, the default mixin methods need
1117 a way to create new instances from an iterable. The class constructor is
1118 assumed to have a signature in the form ``ClassName(iterable)``.
1119 That assumption is factored-out to an internal classmethod called
1120 :meth:`_from_iterable` which calls ``cls(iterable)`` to produce a new set.
1121 If the :class:`Set` mixin is being used in a class with a different
Raymond Hettingere5820c62011-03-22 09:11:39 -07001122 constructor signature, you will need to override :meth:`_from_iterable`
Raymond Hettingera6b76ba2010-08-08 00:29:08 +00001123 with a classmethod that can construct new instances from
1124 an iterable argument.
1125
1126(2)
1127 To override the comparisons (presumably for speed, as the
1128 semantics are fixed), redefine :meth:`__le__` and
1129 then the other operations will automatically follow suit.
1130
1131(3)
1132 The :class:`Set` mixin provides a :meth:`_hash` method to compute a hash value
1133 for the set; however, :meth:`__hash__` is not defined because not all sets
1134 are hashable or immutable. To add set hashabilty using mixins,
1135 inherit from both :meth:`Set` and :meth:`Hashable`, then define
1136 ``__hash__ = Set._hash``.
1137
1138.. seealso::
1139
1140 * `OrderedSet recipe <http://code.activestate.com/recipes/576694/>`_ for an
1141 example built on :class:`MutableSet`.
1142
1143 * For more about ABCs, see the :mod:`abc` module and :pep:`3119`.