preview

Essay On Deque

Good Essays

A deque object is similar to a stack or a queue and is initialized left-to-right from an iterable value (or empty). In case you're wondering, deque is pronounced "deck" and is short for a double-ended queue.

Deques are thread-safe and memory efficient. They can be appended, extended or "popped" from either side. The performance when performing these operations from one side to the next is roughly the same. Lists of course are faster since they are immutable but deques are slightly faster than standard tuples.

If you don't specify a size (using maxlen or the 2nd positional argument of deque()), deques can be as large as you want them to be. If you do specify a size, you can still use add and delete operations but when one side is added, …show more content…

Eventually, it will get the last element from the deque of another processor and execute it until all deques are cleared (though usually with a processor, the work is continuous and more efficient due to the algorithm).

No worries though, we will just go through some simple examples from the Python interpreter.

The datatype "deque" comes from the collections package. So we need to import it from there:

>>> from collections import deque
Much like the namedtuple type, we can give it a string and it will be extended into an array-like structure like so:

>>> my_deque = deque('long')

>>> print(type(my_deque))

>>> print(my_deque) deque(['l', 'o', 'n', 'g'])
If we want to add to the right, we can use append() just like you would with a normal tuple:

>>> my_deque.append('d')
We can also add to the left side with appendleft():

>>> my_deque.appendleft('s') and continue on from there:

>>> my_deque.append('e')

>>> my_deque.appendleft('i')

>>> my_deque.append('q')

>>> my_deque.appendleft('h')

>>> my_deque.append('u')

>>> my_deque.appendleft('t')

>>> my_deque.append('e')

>>> print(my_deque) deque(['t', 'h', 'i', 's', 'l', 'o', 'n', 'g', 'd', 'e', 'q', 'u', 'e'])
We can also make shallow copies with the copy() method:

>>> deque_copy = my_deque.copy()

>>> print(deque_copy) deque(['t', 'h', 'i', 's', 'l', 'o', 'n', 'g', 'd', 'e', 'q', 'u', 'e'])
We can then clear them from all elements by using clear():

>>> deque_copy.clear()

>>> print(deque_copy) deque([]) To get a

Get Access