goodcow
goodcow

Reputation: 4795

Python deque: difference from list?

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?

Upvotes: 17

Views: 9110

Answers (4)

Gayal Kuruppu
Gayal Kuruppu

Reputation: 1391

This will be really helpful in addition to the answers given above. From the documentation:

It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the other elements have to be shifted by one)

For more details about time complexity analysis: https://wiki.python.org/moin/TimeComplexity

Upvotes: 1

Garf
Garf

Reputation: 21

A list is supposed to be iterated and/or randomly accessed. Typical usage: store a collection of homogeneous data items.

A queue is designed to be operated at the end/beginning. Typical usage: store data with priority information, facilitating a wide/depth-first search.

Upvotes: 2

edwardtoday
edwardtoday

Reputation: 373

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.

Upvotes: 10

Eevee
Eevee

Reputation: 48564

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.

Upvotes: 22

Related Questions