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

后端 未结 5 1692
南方客
南方客 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:40

    https://github.com/python/cpython/blob/v3.8.1/Modules/_collectionsmodule.c

    A dequeobject is composed of a doubly-linked list of block nodes.

    So yes, a deque is a (doubly-)linked list as another answer suggests.

    Elaborating: What this means is that Python lists are much better for random-access and fixed-length operations, including slicing, while deques are much more useful for pushing and popping things off the ends, with indexing (but not slicing, interestingly) being possible but slower than with lists.

提交回复
热议问题