Deques support thread-safe, memory efficient it work very similarly to Arrays, but they are more used to simulate stacks. You can import the deque library at the beginning of the code import collections.
You can declare a deque:
Deques are indexed by integers starting on position 0 and until n-1, being n the length of the deque.
Example:
Has length 5 and can be indexed like this:
0 | 1 | 2 | 3 | 4 |
output:
Which will print out every member of deque1.
But we can also access it with negative numbers which do it from the end to the start. Like this:
-5 | -4 | -3 | -2 | -1 |
Output:
This will print the deque1 backwards.
Or we can also use a for to iterate over the Array.
Output:
We can also concatenate Deques by adding them.
Example:
Output:
Will print a bigger Deque containing deque1 and deque2
We can repeat the contents by x times.
Example:
Will print the deque1 tripled.
There is a way to finding out if a specific value exists in a specific Deque.
Example:
This will print out True because integer 2 indeed exists in Deque1.
Using the Membership operator, we can iterate over every member of a Deque.
Output:
For each iteration of this loop, the temporary variable member will be assigned a value of the Deque being printed out and passing on to the next value.
To get the size of a specific Deque. Using the method len().
Example:
This code will print out the size of deque1 which is 5.
Deques can be sliced to show only desired values or to iterate over desired indexes.
First-line will print from index 1 to 3 and the second will print starting on index 2.
Deques are a generalization of stacks and queues as ``deck'' 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, so even though lists support very similar operations, deques are more efficient. Deques are also good to simulate a stack or ram memory.
If you don't need insertion at the head of the list, then a circular deque gives unnecessary overhead, e.g. for indexed methods. In a circular deque, accessing index i requires adding i to the index of the head element and looping around (e.g. using modulo, or bitwise operators) before accessing the underlying array, whereas, with a vanilla vector, no manipulation to i is necessary before accessing the underlying array.
Function |
Description |
E.g |
len(Tuple) |
It is used to calculate the length of the Deque. |
len(deque1) will return 3 |
max(Tuple) |
It returns the maximum element of the Deque. |
max(deque1) will return 8 |
min(Tuple) |
It returns the minimum element of the Deque. |
min(deque1) will return 1 |
deque(seq) |
It converts a list or tuple to a Deque. |
deque1([1,2,3,4,5,6,7]) will create a deque containing that lists’ elements. |
Function |
Description |
E.g |
deque.append(obj) |
The object obj is added to the Deque. |
deque1.append(6) Will add the integer 6 to deque1 |
deque.appendleft(obj) |
Adds obj to the left side of the Deque. |
print(deque1.appendleft(0)) Will add the integer 0 to the left side of deque1. |
deque.clear() |
Removes all elements from the Deque. |
deque1.clear() clears the entire deque. |
deque.extend(seq) |
Object seq is extended to the Array. |
deque1.extend((6,7,8,9,10)) It extends deque1 adding those 5 elements. |
deque.extendleft(seq) |
It returns the lowest index in the Array that object appears. |
print(deque1.extendleft((-2,-1,0)) it extends deque1 to the left with -2, -1 and 0. |
deque.pop() |
It removes and returns the last object of the deque. |
print(de.pop()) Will remove the integer 5 and print it. |
deque.popleft() |
Remove and return an element from the left side of the deque. |
print(deque1.popleft()) Will remove the integer 1 and print it. |
deque.remove(obj) |
It removes the specified object. |
deque1.remove(3) Removes the integer 3 from Deque1. |
deque.rotate(n) |
Rotate the deque n steps to the right. It rotates to the left if n is negative. |
deque1.rotate(1) Rotates the entire deque one step to the right. deque1.appendleft(deque1.pop())produces the same result as rotating one step to the right. |