How are deques in Python implemented, and when are they worse than lists?

后端 未结 5 1691
南方客
南方客 2020-11-28 04:48

I\'ve recently gotten into investigating how various data structures are implemented in Python in order to make my code more efficient. In investigating how lists and deques

5条回答
  •  遥遥无期
    2020-11-28 05:35

    The documentation entry for deque objects spells out most of what you need to know, I suspect. Notable quotes:

    Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

    But...

    Indexed access is O(1) at both ends but slows to O(n) in the middle. For fast random access, use lists instead.

    I'd have to take a look at the source to tell whether the implementation is a linked list or something else, but it sounds to me as though a deque has roughly the same characteristics as a doubly-linked list.

提交回复
热议问题