blob: 55ab43108649be79f15b6a0feb40c054ae203923 [file] [log] [blame]
Raymond Hettingere52f3b12004-01-29 07:27:45 +00001\section{\module{collections} ---
2 High-performance datatypes}
3
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
11This module implements high-performance datatypes. Currently, the
12only 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
20 Deques are a generalization of stacks and queues. They support
21 thread-safe, memory efficient appends and pops from either side of the
22 deque with approximately the same performance in either direction.
23 \versionadded{2.4}
24\end{funcdesc}
25
26Deque objects support the following methods:
27
28\begin{methoddesc}{append}{x}
29 Add \var{x} to the right side of the deque.
30\end{methoddesc}
31
32\begin{methoddesc}{appendleft}{x}
33 Add \var{x} to the left side of the deque.
34\end{methoddesc}
35
36\begin{methoddesc}{clear}{}
37 Remove all elements from the deque leaving it with length 0.
38\end{methoddesc}
39
Raymond Hettinger3ba85c22004-02-06 19:04:56 +000040\begin{methoddesc}{extend}{iterable}
41 Extend the right side of the deque by appending elements from
42 the iterable argument.
43\end{methoddesc}
44
45\begin{methoddesc}{extendleft}{iterable}
46 Extend the left side of the deque by appending elements from
47 \var{iterable}. Note, the series of left appends results in
48 reversing the order of elements in the iterable argument.
49\end{methoddesc}
50
Raymond Hettingere52f3b12004-01-29 07:27:45 +000051\begin{methoddesc}{pop}{}
52 Remove and return an element from the right side of the deque.
53 If no elements are present, raises a \exception{LookupError}.
54\end{methoddesc}
55
56\begin{methoddesc}{popleft}{}
57 Remove and return an element from the left side of the deque.
58 If no elements are present, raises a \exception{LookupError}.
59\end{methoddesc}
60
61In addition to the above, deques support iteration, membership testing
62using the \keyword{in} operator, \samp{len(d)}, \samp{copy.copy(d)},
Raymond Hettingerc058fd12004-02-07 02:45:22 +000063\samp{copy.deepcopy(d)}, \samp{reversed(d)} and pickling.
Raymond Hettingere52f3b12004-01-29 07:27:45 +000064
65Example:
66
67\begin{verbatim}
68>>> from collections import deque
69>>> d = deque('ghi') # make a new deque with three items
70>>> for elem in d: # iterate over the deque's elements
71 print elem.upper()
72
73
74G
75H
76I
77>>> d.append('j') # add a new entry to the right side
78>>> d.appendleft('f') # add a new entry to the left side
79>>> d # show the representation of the deque
80deque(['f', 'g', 'h', 'i', 'j'])
81>>> d.pop() # return and remove the rightmost item
82'j'
83>>> d.popleft() # return and remove the leftmost item
84'f'
85>>> list(d) # list the contents of the deque
86['g', 'h', 'i']
Raymond Hettingerc058fd12004-02-07 02:45:22 +000087>>> list(reversed(d)) # list the contents of a deque in reverse
88['i', 'h', 'g']
Raymond Hettingere52f3b12004-01-29 07:27:45 +000089>>> 'h' in d # search the deque
90True
Raymond Hettinger3ba85c22004-02-06 19:04:56 +000091>>> d.extend('jkl') # extend() will append many elements at once
Raymond Hettingere52f3b12004-01-29 07:27:45 +000092>>> d
93deque(['g', 'h', 'i', 'j', 'k', 'l'])
94>>> d.clear() # empty the deque
Raymond Hettinger3ba85c22004-02-06 19:04:56 +000095>>> d.pop() # cannot pop from an empty deque
Raymond Hettingere52f3b12004-01-29 07:27:45 +000096
97Traceback (most recent call last):
98 File "<pyshell#6>", line 1, in -toplevel-
99 d.pop()
100LookupError: pop from an empty deque
Raymond Hettinger3ba85c22004-02-06 19:04:56 +0000101
102>>> d.extendleft('abc') # extendleft() reverses the element order
103>>> d
104deque(['c', 'b', 'a'])
105
Raymond Hettingere52f3b12004-01-29 07:27:45 +0000106\end{verbatim}