Efficiency of using a Python list as a queue

后端 未结 5 2043
南笙
南笙 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:16

    Every .pop(0) takes N steps, since the list has to be reorganized. The required memory will not grow endlessly and only be as big as required for the items that are held.

    I'd recommend using deque to get O(1) append and pop from front.

提交回复
热议问题