blob: 542ef6b0e96c904f4c634f80083af90892c059ae [file] [log] [blame]
Raymond Hettingere52f3b12004-01-29 07:27:45 +00001\section{\module{collections} ---
Raymond Hettinger5c5eb862004-02-07 21:13:00 +00002 High-performance container datatypes}
Raymond Hettingere52f3b12004-01-29 07:27:45 +00003
4\declaremodule{standard}{collections}
5\modulesynopsis{High-performance datatypes}
6\moduleauthor{Raymond Hettinger}{python@rcn.com}
7\sectionauthor{Raymond Hettinger}{python@rcn.com}
8\versionadded{2.4}
9
10
Guido van Rossum1968ad32006-02-25 22:38:04 +000011This module implements high-performance container datatypes. Currently,
12there are two datatypes, deque and defaultdict.
13Future additions may include B-trees and Fibonacci heaps.
14\versionchanged[Added defaultdict]{2.5}
Raymond Hettingere52f3b12004-01-29 07:27:45 +000015
16\begin{funcdesc}{deque}{\optional{iterable}}
17 Returns a new deque objected initialized left-to-right (using
18 \method{append()}) with data from \var{iterable}. If \var{iterable}
19 is not specified, the new deque is empty.
20
Raymond Hettinger5c5eb862004-02-07 21:13:00 +000021 Deques are a generalization of stacks and queues (the name is pronounced
22 ``deck'' and is short for ``double-ended queue''). Deques support
23 thread-safe, memory efficient appends and pops from either side of the deque
24 with approximately the same \code{O(1)} performance in either direction.
25
26 Though \class{list} objects support similar operations, they are optimized
27 for fast fixed-length operations and incur \code{O(n)} memory movement costs
28 for \samp{pop(0)} and \samp{insert(0, v)} operations which change both the
29 size and position of the underlying data representation.
Raymond Hettingere52f3b12004-01-29 07:27:45 +000030 \versionadded{2.4}
31\end{funcdesc}
32
33Deque objects support the following methods:
34
35\begin{methoddesc}{append}{x}
36 Add \var{x} to the right side of the deque.
37\end{methoddesc}
38
39\begin{methoddesc}{appendleft}{x}
40 Add \var{x} to the left side of the deque.
41\end{methoddesc}
42
43\begin{methoddesc}{clear}{}
44 Remove all elements from the deque leaving it with length 0.
45\end{methoddesc}
46
Raymond Hettinger3ba85c22004-02-06 19:04:56 +000047\begin{methoddesc}{extend}{iterable}
48 Extend the right side of the deque by appending elements from
49 the iterable argument.
50\end{methoddesc}
51
52\begin{methoddesc}{extendleft}{iterable}
53 Extend the left side of the deque by appending elements from
54 \var{iterable}. Note, the series of left appends results in
55 reversing the order of elements in the iterable argument.
56\end{methoddesc}
57
Raymond Hettingere52f3b12004-01-29 07:27:45 +000058\begin{methoddesc}{pop}{}
59 Remove and return an element from the right side of the deque.
Raymond Hettinger738ec902004-02-29 02:15:56 +000060 If no elements are present, raises a \exception{IndexError}.
Raymond Hettingere52f3b12004-01-29 07:27:45 +000061\end{methoddesc}
62
63\begin{methoddesc}{popleft}{}
64 Remove and return an element from the left side of the deque.
Raymond Hettinger738ec902004-02-29 02:15:56 +000065 If no elements are present, raises a \exception{IndexError}.
66\end{methoddesc}
67
Raymond Hettinger4aec61e2005-03-18 21:20:23 +000068\begin{methoddesc}{remove}{value}
69 Removed the first occurrence of \var{value}. If not found,
70 raises a \exception{ValueError}.
71 \versionadded{2.5}
72\end{methoddesc}
73
Raymond Hettinger5c5eb862004-02-07 21:13:00 +000074\begin{methoddesc}{rotate}{n}
75 Rotate the deque \var{n} steps to the right. If \var{n} is
76 negative, rotate to the left. Rotating one step to the right
Raymond Hettingerf5f9a3702004-04-30 22:52:50 +000077 is equivalent to: \samp{d.appendleft(d.pop())}.
Raymond Hettinger5c5eb862004-02-07 21:13:00 +000078\end{methoddesc}
79
80In addition to the above, deques support iteration, pickling, \samp{len(d)},
Raymond Hettinger0a4977c2004-03-01 23:16:22 +000081\samp{reversed(d)}, \samp{copy.copy(d)}, \samp{copy.deepcopy(d)},
82membership testing with the \keyword{in} operator, and subscript references
83such as \samp{d[-1]}.
Raymond Hettingere52f3b12004-01-29 07:27:45 +000084
85Example:
86
87\begin{verbatim}
88>>> from collections import deque
Raymond Hettinger5c5eb862004-02-07 21:13:00 +000089>>> d = deque('ghi') # make a new deque with three items
90>>> for elem in d: # iterate over the deque's elements
Raymond Hettinger738ec902004-02-29 02:15:56 +000091... print elem.upper()
Raymond Hettingere52f3b12004-01-29 07:27:45 +000092G
93H
94I
Raymond Hettinger738ec902004-02-29 02:15:56 +000095
Raymond Hettinger5c5eb862004-02-07 21:13:00 +000096>>> d.append('j') # add a new entry to the right side
97>>> d.appendleft('f') # add a new entry to the left side
98>>> d # show the representation of the deque
Raymond Hettingere52f3b12004-01-29 07:27:45 +000099deque(['f', 'g', 'h', 'i', 'j'])
Raymond Hettinger738ec902004-02-29 02:15:56 +0000100
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000101>>> d.pop() # return and remove the rightmost item
Raymond Hettingere52f3b12004-01-29 07:27:45 +0000102'j'
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000103>>> d.popleft() # return and remove the leftmost item
Raymond Hettingere52f3b12004-01-29 07:27:45 +0000104'f'
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000105>>> list(d) # list the contents of the deque
Raymond Hettingere52f3b12004-01-29 07:27:45 +0000106['g', 'h', 'i']
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000107>>> d[0] # peek at leftmost item
Raymond Hettinger738ec902004-02-29 02:15:56 +0000108'g'
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000109>>> d[-1] # peek at rightmost item
Raymond Hettinger738ec902004-02-29 02:15:56 +0000110'i'
Raymond Hettinger0a4977c2004-03-01 23:16:22 +0000111
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000112>>> list(reversed(d)) # list the contents of a deque in reverse
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000113['i', 'h', 'g']
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000114>>> 'h' in d # search the deque
Raymond Hettingere52f3b12004-01-29 07:27:45 +0000115True
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000116>>> d.extend('jkl') # add multiple elements at once
Raymond Hettingere52f3b12004-01-29 07:27:45 +0000117>>> d
118deque(['g', 'h', 'i', 'j', 'k', 'l'])
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000119>>> d.rotate(1) # right rotation
120>>> d
121deque(['l', 'g', 'h', 'i', 'j', 'k'])
122>>> d.rotate(-1) # left rotation
123>>> d
124deque(['g', 'h', 'i', 'j', 'k', 'l'])
Raymond Hettinger738ec902004-02-29 02:15:56 +0000125
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000126>>> deque(reversed(d)) # make a new deque in reverse order
127deque(['l', 'k', 'j', 'i', 'h', 'g'])
128>>> d.clear() # empty the deque
129>>> d.pop() # cannot pop from an empty deque
Raymond Hettingere52f3b12004-01-29 07:27:45 +0000130Traceback (most recent call last):
131 File "<pyshell#6>", line 1, in -toplevel-
132 d.pop()
Raymond Hettinger738ec902004-02-29 02:15:56 +0000133IndexError: pop from an empty deque
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000134
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000135>>> d.extendleft('abc') # extendleft() reverses the input order
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000136>>> d
137deque(['c', 'b', 'a'])
Raymond Hettingerf5f9a3702004-04-30 22:52:50 +0000138\end{verbatim}
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000139
Raymond Hettingere7169eb2004-05-09 01:15:01 +0000140\subsection{Recipes \label{deque-recipes}}
141
142This section shows various approaches to working with deques.
143
144The \method{rotate()} method provides a way to implement \class{deque}
Raymond Hettinger2e669402004-06-12 07:59:40 +0000145slicing and deletion. For example, a pure python implementation of
146\code{del d[n]} relies on the \method{rotate()} method to position
147elements to be popped:
148
Raymond Hettingere7169eb2004-05-09 01:15:01 +0000149\begin{verbatim}
150def delete_nth(d, n):
Raymond Hettingere7169eb2004-05-09 01:15:01 +0000151 d.rotate(-n)
152 d.popleft()
153 d.rotate(n)
Raymond Hettingere7169eb2004-05-09 01:15:01 +0000154\end{verbatim}
155
Raymond Hettinger0e371f22004-05-12 20:55:56 +0000156To implement \class{deque} slicing, use a similar approach applying
157\method{rotate()} to bring a target element to the left side of the deque.
158Remove old entries with \method{popleft()}, add new entries with
159\method{extend()}, and then reverse the rotation.
Raymond Hettingere7169eb2004-05-09 01:15:01 +0000160
161With minor variations on that approach, it is easy to implement Forth style
162stack manipulations such as \code{dup}, \code{drop}, \code{swap}, \code{over},
163\code{pick}, \code{rot}, and \code{roll}.
Raymond Hettingerf5f9a3702004-04-30 22:52:50 +0000164
165A roundrobin task server can be built from a \class{deque} using
166\method{popleft()} to select the current task and \method{append()}
167to add it back to the tasklist if the input stream is not exhausted:
168
169\begin{verbatim}
170def roundrobin(*iterables):
171 pending = deque(iter(i) for i in iterables)
172 while pending:
173 task = pending.popleft()
174 try:
175 yield task.next()
176 except StopIteration:
177 continue
178 pending.append(task)
179
180>>> for value in roundrobin('abc', 'd', 'efgh'):
Raymond Hettingere7169eb2004-05-09 01:15:01 +0000181... print value
Raymond Hettingerf5f9a3702004-04-30 22:52:50 +0000182
183a
184d
185e
186b
187f
188c
189g
190h
191
192\end{verbatim}
Raymond Hettingere7169eb2004-05-09 01:15:01 +0000193
194
195Multi-pass data reduction algorithms can be succinctly expressed and
Raymond Hettinger2e669402004-06-12 07:59:40 +0000196efficiently coded by extracting elements with multiple calls to
197\method{popleft()}, applying the reduction function, and calling
198\method{append()} to add the result back to the queue.
Raymond Hettingere7169eb2004-05-09 01:15:01 +0000199
200For example, building a balanced binary tree of nested lists entails
201reducing two adjacent nodes into one by grouping them in a list:
202
203\begin{verbatim}
204def maketree(iterable):
205 d = deque(iterable)
206 while len(d) > 1:
207 pair = [d.popleft(), d.popleft()]
208 d.append(pair)
209 return list(d)
210
211>>> print maketree('abcdefgh')
212[[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
213
214\end{verbatim}
Guido van Rossum1968ad32006-02-25 22:38:04 +0000215
216
217
218\begin{funcdesc}{defaultdict}{\optional{default_factory\optional{, ...}}}
219 Returns a new dictionary-like object. \class{defaultdict} is a subclass
220 of the builtin \class{dict} class. It overrides one method and adds one
221 writable instance variable. The remaining functionality is the same as
222 for the \class{dict} class and is not documented here.
223
224 The first argument provides the initial value for the
225 \member{default_factory} attribute; it defaults to \code{None}.
226 All remaining arguments are treated the same as if they were
227 passed to the \class{dict} constructor, including keyword arguments.
228
229 \versionadded{2.5}
230\end{funcdesc}
231
232\class{defaultdict} objects support the following method in addition to
233the standard \class{dict} operations:
234
235\begin{methoddesc}{__missing__}{key}
236 If the \member{default_factory} attribute is \code{None}, this raises
237 an \exception{KeyError} exception with the \var{key} as argument.
238
239 If \member{default_factory} is not \code{None}, it is called without
240 arguments to provide a default value for the given \var{key}, this
241 value is inserted in the dictionary for the \var{key}, and returned.
242
243 If calling \member{default_factory} raises an exception this exception
244 is propagated unchanged.
245
246 This method is called by the \method{__getitem__} method of the
247 \class{dict} class when the requested key is not found; whatever it
248 returns or raises is then returned or raised by \method{__getitem__}.
249\end{methoddesc}
250
251\class{defaultdict} objects support the following instance variable:
252
253\begin{datadesc}{default_factory}
254 This attribute is used by the \method{__missing__} method; it is initialized
255 from the first argument to the constructor, if present, or to \code{None},
256 if absent.
257\end{datadesc}