blob: 279309578904c7115e460af2d6168cc8f35695e1 [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
Raymond Hettinger5c5eb862004-02-07 21:13:00 +000011This module implements high-performance container datatypes. Currently, the
Raymond Hettingere52f3b12004-01-29 07:27:45 +000012only datatype is a deque. Future additions may include B-trees
13and Fibonacci heaps.
14
15\begin{funcdesc}{deque}{\optional{iterable}}
16 Returns a new deque objected initialized left-to-right (using
17 \method{append()}) with data from \var{iterable}. If \var{iterable}
18 is not specified, the new deque is empty.
19
Raymond Hettinger5c5eb862004-02-07 21:13:00 +000020 Deques are a generalization of stacks and queues (the name is pronounced
21 ``deck'' and is short for ``double-ended queue''). Deques support
22 thread-safe, memory efficient appends and pops from either side of the deque
23 with approximately the same \code{O(1)} performance in either direction.
24
25 Though \class{list} objects support similar operations, they are optimized
26 for fast fixed-length operations and incur \code{O(n)} memory movement costs
27 for \samp{pop(0)} and \samp{insert(0, v)} operations which change both the
28 size and position of the underlying data representation.
Raymond Hettingere52f3b12004-01-29 07:27:45 +000029 \versionadded{2.4}
30\end{funcdesc}
31
32Deque objects support the following methods:
33
34\begin{methoddesc}{append}{x}
35 Add \var{x} to the right side of the deque.
36\end{methoddesc}
37
38\begin{methoddesc}{appendleft}{x}
39 Add \var{x} to the left side of the deque.
40\end{methoddesc}
41
42\begin{methoddesc}{clear}{}
43 Remove all elements from the deque leaving it with length 0.
44\end{methoddesc}
45
Raymond Hettinger3ba85c22004-02-06 19:04:56 +000046\begin{methoddesc}{extend}{iterable}
47 Extend the right side of the deque by appending elements from
48 the iterable argument.
49\end{methoddesc}
50
51\begin{methoddesc}{extendleft}{iterable}
52 Extend the left side of the deque by appending elements from
53 \var{iterable}. Note, the series of left appends results in
54 reversing the order of elements in the iterable argument.
55\end{methoddesc}
56
Raymond Hettinger738ec902004-02-29 02:15:56 +000057\begin{methoddesc}{left}{}
58 Return leftmost element from the deque.
59 If no elements are present, raises a \exception{IndexError}.
60\end{methoddesc}
61
Raymond Hettingere52f3b12004-01-29 07:27:45 +000062\begin{methoddesc}{pop}{}
63 Remove and return an element from the right side of the deque.
Raymond Hettinger738ec902004-02-29 02:15:56 +000064 If no elements are present, raises a \exception{IndexError}.
Raymond Hettingere52f3b12004-01-29 07:27:45 +000065\end{methoddesc}
66
67\begin{methoddesc}{popleft}{}
68 Remove and return an element from the left side of the deque.
Raymond Hettinger738ec902004-02-29 02:15:56 +000069 If no elements are present, raises a \exception{IndexError}.
70\end{methoddesc}
71
72\begin{methoddesc}{right}{}
73 Return the rightmost element from the deque.
74 If no elements are present, raises a \exception{IndexError}.
Raymond Hettingere52f3b12004-01-29 07:27:45 +000075\end{methoddesc}
76
Raymond Hettinger5c5eb862004-02-07 21:13:00 +000077\begin{methoddesc}{rotate}{n}
78 Rotate the deque \var{n} steps to the right. If \var{n} is
79 negative, rotate to the left. Rotating one step to the right
80 is equivalent to: \samp{d.appendleft(d.pop())}.
81\end{methoddesc}
82
83In addition to the above, deques support iteration, pickling, \samp{len(d)},
84\samp{reversed(d)}, \samp{copy.copy(d)}, \samp{copy.deepcopy(d)}, and
85membership testing with the \keyword{in} operator.
Raymond Hettingere52f3b12004-01-29 07:27:45 +000086
87Example:
88
89\begin{verbatim}
90>>> from collections import deque
Raymond Hettinger5c5eb862004-02-07 21:13:00 +000091>>> d = deque('ghi') # make a new deque with three items
92>>> for elem in d: # iterate over the deque's elements
Raymond Hettinger738ec902004-02-29 02:15:56 +000093... print elem.upper()
Raymond Hettingere52f3b12004-01-29 07:27:45 +000094G
95H
96I
Raymond Hettinger738ec902004-02-29 02:15:56 +000097
Raymond Hettinger5c5eb862004-02-07 21:13:00 +000098>>> d.append('j') # add a new entry to the right side
99>>> d.appendleft('f') # add a new entry to the left side
100>>> d # show the representation of the deque
Raymond Hettingere52f3b12004-01-29 07:27:45 +0000101deque(['f', 'g', 'h', 'i', 'j'])
Raymond Hettinger738ec902004-02-29 02:15:56 +0000102
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000103>>> d.pop() # return and remove the rightmost item
Raymond Hettingere52f3b12004-01-29 07:27:45 +0000104'j'
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000105>>> d.popleft() # return and remove the leftmost item
Raymond Hettingere52f3b12004-01-29 07:27:45 +0000106'f'
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000107>>> list(d) # list the contents of the deque
Raymond Hettingere52f3b12004-01-29 07:27:45 +0000108['g', 'h', 'i']
Raymond Hettinger738ec902004-02-29 02:15:56 +0000109
110>>> d.left() # peek at leftmost item
111'g'
112>>> d.right() # peek at rightmost item
113'i'
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000114>>> list(reversed(d)) # list the contents of a deque in reverse
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000115['i', 'h', 'g']
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000116>>> 'h' in d # search the deque
Raymond Hettingere52f3b12004-01-29 07:27:45 +0000117True
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000118>>> d.extend('jkl') # add multiple elements at once
Raymond Hettingere52f3b12004-01-29 07:27:45 +0000119>>> d
120deque(['g', 'h', 'i', 'j', 'k', 'l'])
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000121>>> d.rotate(1) # right rotation
122>>> d
123deque(['l', 'g', 'h', 'i', 'j', 'k'])
124>>> d.rotate(-1) # left rotation
125>>> d
126deque(['g', 'h', 'i', 'j', 'k', 'l'])
Raymond Hettinger738ec902004-02-29 02:15:56 +0000127
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000128>>> deque(reversed(d)) # make a new deque in reverse order
129deque(['l', 'k', 'j', 'i', 'h', 'g'])
130>>> d.clear() # empty the deque
131>>> d.pop() # cannot pop from an empty deque
Raymond Hettingere52f3b12004-01-29 07:27:45 +0000132Traceback (most recent call last):
133 File "<pyshell#6>", line 1, in -toplevel-
134 d.pop()
Raymond Hettinger738ec902004-02-29 02:15:56 +0000135IndexError: pop from an empty deque
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000136
Raymond Hettinger5c5eb862004-02-07 21:13:00 +0000137>>> d.extendleft('abc') # extendleft() reverses the input order
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000138>>> d
139deque(['c', 'b', 'a'])
140
Raymond Hettingere52f3b12004-01-29 07:27:45 +0000141\end{verbatim}