diff options
author | Ezio Melotti <ezio.melotti@gmail.com> | 2010-03-31 07:26:24 (GMT) |
---|---|---|
committer | Ezio Melotti <ezio.melotti@gmail.com> | 2010-03-31 07:26:24 (GMT) |
commit | eb72991fbbb4af101b047fa380d61dc3934d5721 (patch) | |
tree | d75a8d8a67457243aa2b8e831f0a2946aeee9032 /Doc/tutorial | |
parent | aeb2e8295d74013662edf8ebba2a9aac24ee7d3c (diff) | |
download | cpython-eb72991fbbb4af101b047fa380d61dc3934d5721.zip cpython-eb72991fbbb4af101b047fa380d61dc3934d5721.tar.gz cpython-eb72991fbbb4af101b047fa380d61dc3934d5721.tar.bz2 |
Revert r79179 and merge r75584 to explain how to implement a queue using collection.deque instead of a list.
Diffstat (limited to 'Doc/tutorial')
-rw-r--r-- | Doc/tutorial/datastructures.rst | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/Doc/tutorial/datastructures.rst b/Doc/tutorial/datastructures.rst index 669dfc4..dfc2b33 100644 --- a/Doc/tutorial/datastructures.rst +++ b/Doc/tutorial/datastructures.rst @@ -138,26 +138,25 @@ Using Lists as Queues .. sectionauthor:: Ka-Ping Yee <ping@lfw.org> +It is also possible to use a list as a queue, where the first element added is +the first element retrieved ("first-in, first-out"); however, lists are not +efficient for this purpose. While appends and pops from the end of list are +fast, doing inserts or pops from the beginning of a list is slow (because all +of the other elements have to be shifted by one). -You can also use a list conveniently as a queue, where the first element added -is the first element retrieved ("first-in, first-out"). To add an item to the -back of the queue, use :meth:`append`. To retrieve an item from the front of -the queue, use :meth:`pop` with ``0`` as the index. For example:: +To implement a queue, use :class:`collections.deque` which was designed to +have fast appends and pops from both ends. For example:: - >>> queue = ["Eric", "John", "Michael"] + >>> from collections import deque + >>> queue = deque(["Eric", "John", "Michael"]) >>> queue.append("Terry") # Terry arrives >>> queue.append("Graham") # Graham arrives - >>> queue.pop(0) + >>> queue.popleft() # The first to arrive now leaves 'Eric' - >>> queue.pop(0) + >>> queue.popleft() # The second to arrive now leaves 'John' - >>> queue - ['Michael', 'Terry', 'Graham'] - -However, since lists are implemented as an array of elements, they are not the -optimal data structure to use as a queue (the ``pop(0)`` needs to move all -following elements). See :ref:`tut-list-tools` for a look at -:class:`collections.deque`, which is designed to work efficiently as a queue. + >>> queue # Remaining queue in order of arrival + deque(['Michael', 'Terry', 'Graham']) .. _tut-functional: |