Is collection.deque the best implementation of constant length list in Python?

Question:

I want to limit the length of the list in python, when len(list) > limit, the first item will be removed, collection.deque can achieve it, however, will it be slower than:

list_A = [2,4,6,8,11]
length_limit = 5
while True:
    # do something to the list, for example, list_A.append(2)
    if len(list_A) > length_limit:
        list_A = list_A[1:]

Is any other way to achieve it much readable and efficient than collection.deque?

Asked By: tung

||

Answers:

A fact you need to know is that list and deque are different implementations at the bottom. In Cpython, the former is a dynamic array, and the latter is a doubly linked list. Arrays are good at accessing element through indexes but are not good at inserting or removing elements from inside. On the contrary, linked lists are good at inserting or removing elements from anywhere (but the node should be determined first) but are not good at accessing element through indexes.

As for what you have described so far, if you don’t need your list to have efficient random access, deque is indeed the most suitable choice. When you use slicing for a list, python will copy the references in the list according to the range specified by the slicing and store them in a new list. In your case, this is an O(n) operation (that is, the time required is proportional to the length of the list). For deque, when the length exceeds the specified maxlen, It only needs to simply discard the header node (Cpython also optimizes deque with maxlen restriction). This is an O(1) operation, which is obviously more efficient than the list.

Answered By: Mechanic Pig