Efficiency of using a Python list as a queue

后端 未结 5 2042
南笙
南笙 2021-01-30 00:51

A coworker recently wrote a program in which he used a Python list as a queue. In other words, he used .append(x) when needing to insert items and .pop(0)

5条回答
  •  北海茫月
    2021-01-30 01:23

    You won't run out of memory using the list implementation, but performance will be poor. From the docs:

    Though list objects support similar operations, they are optimized for fast fixed-length operations and incur O(n) memory movement costs for pop(0) and insert(0, v) operations which change both the size and position of the underlying data representation.

    So using a deque will be much faster.

提交回复
热议问题