Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python deque: difference from list?

Tags:

python

I'm reading the Python Documentation: I don't understand how a deque is different from a list. From the documentation:

Returns a new deque object initialized left-to-right (using append()) with data from iterable. If iterable is not specified, the new deque is empty.

Deques are a generalization of stacks and queues (the name is pronounced “deck” and is short for “double-ended queue”). 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.

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.

If a deque is basically a list, but more efficient, why would one use a list in place of a deque?

like image 564
goodcow Avatar asked Mar 12 '14 01:03

goodcow


People also ask

Is deque better than list in Python?

A list is one of the main workhorses in almost every Python script, yet, in some cases, opting for deques can lead to much faster performance. A deque is short for a double-ended queue, which is why it's called so. Double-ended means that it supports adding and removing elements from both ends.

How is a deque different from a list?

A deque is a set of linked memory blocks, where more than one element is stored in each memory block. A list is a set of elements dispersed in memory, i.e.: only one element is stored per memory "block".

What are the advantages of using deque over list?

One of the biggest advantages of deque over a list is the possibility to append and delete items from the left end. As mentioned earlier, a deque object is much more efficient for these operations at the left end, especially as the size of the queue increases.

Is Python deque linked list?

deque uses a linked list as part of its data structure. This is the kind of linked list it uses. With doubly linked lists, deque is capable of inserting or deleting elements from both ends of a queue with constant O(1) performance.


2 Answers

A deque is more efficient for pushing and popping from the ends. Read on, and below the list of methods you'll find:

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

Adding to or removing from the beginning of a list is O(n), but fetching elements from the middle is O(1). For a deque, the reverse is true.

So generally you only want a deque when you don't care about what's in the middle; you want to feed it things in some order and then get them back in that order elsewhere.

like image 113
Eevee Avatar answered Oct 21 '22 05:10

Eevee


Deque is a doubly linked list while List is just an array.

Randomly accessing an object at index i is O(n) for Deque but O(1) for List.

Fast insertions and deletions at the beginning is the biggest advantage of Deque. Fast random reads is the advantage of List.

If insertions and deletions happen randomly in the middle of the container, Deque will have to find the node (O(n), then insert a new node (O(1)), while List having to move some nodes (O(n)).

They both have their use cases.

like image 29
edwardtoday Avatar answered Oct 21 '22 04:10

edwardtoday