Raymond Hettinger | e52f3b1 | 2004-01-29 07:27:45 +0000 | [diff] [blame] | 1 | \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 | |
| 11 | This module implements high-performance datatypes. Currently, the |
| 12 | only datatype is a deque. Future additions may include B-trees |
| 13 | and 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 | |
| 26 | Deque 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 Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 40 | \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 Hettinger | e52f3b1 | 2004-01-29 07:27:45 +0000 | [diff] [blame] | 51 | \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 | |
| 61 | In addition to the above, deques support iteration, membership testing |
| 62 | using the \keyword{in} operator, \samp{len(d)}, \samp{copy.copy(d)}, |
Raymond Hettinger | c058fd1 | 2004-02-07 02:45:22 +0000 | [diff] [blame^] | 63 | \samp{copy.deepcopy(d)}, \samp{reversed(d)} and pickling. |
Raymond Hettinger | e52f3b1 | 2004-01-29 07:27:45 +0000 | [diff] [blame] | 64 | |
| 65 | Example: |
| 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 | |
| 74 | G |
| 75 | H |
| 76 | I |
| 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 |
| 80 | deque(['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 Hettinger | c058fd1 | 2004-02-07 02:45:22 +0000 | [diff] [blame^] | 87 | >>> list(reversed(d)) # list the contents of a deque in reverse |
| 88 | ['i', 'h', 'g'] |
Raymond Hettinger | e52f3b1 | 2004-01-29 07:27:45 +0000 | [diff] [blame] | 89 | >>> 'h' in d # search the deque |
| 90 | True |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 91 | >>> d.extend('jkl') # extend() will append many elements at once |
Raymond Hettinger | e52f3b1 | 2004-01-29 07:27:45 +0000 | [diff] [blame] | 92 | >>> d |
| 93 | deque(['g', 'h', 'i', 'j', 'k', 'l']) |
| 94 | >>> d.clear() # empty the deque |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 95 | >>> d.pop() # cannot pop from an empty deque |
Raymond Hettinger | e52f3b1 | 2004-01-29 07:27:45 +0000 | [diff] [blame] | 96 | |
| 97 | Traceback (most recent call last): |
| 98 | File "<pyshell#6>", line 1, in -toplevel- |
| 99 | d.pop() |
| 100 | LookupError: pop from an empty deque |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 101 | |
| 102 | >>> d.extendleft('abc') # extendleft() reverses the element order |
| 103 | >>> d |
| 104 | deque(['c', 'b', 'a']) |
| 105 | |
Raymond Hettinger | e52f3b1 | 2004-01-29 07:27:45 +0000 | [diff] [blame] | 106 | \end{verbatim} |