Python Deque, Collections Module in Python
The deque in the Python collections module is a double-ended queue data structure. It's implemented as a linked list of blocks, which allows for fast insertions and removals from both ends. Here's some additional information about the deque data structure:
![]() |
| Deque in Python |
- Fast Operations:
- deque operations, such as appending or popping elements from either end, have an average time complexity of O(1). This makes it particularly well-suited for use cases where you need to efficiently add or remove elements from the front or back of the queue.
- Versatile Usage:
- deque can be used to implement various data structures, including queues, stacks, and double-ended queues, making it versatile for a wide range of applications.
- Supports Bounded Length:
- You can create a deque with a maximum length. Once the deque reaches its maximum length, adding new elements will cause the same number of elements to be removed from the opposite end. This is useful for implementing fixed-size circular buffers.
- Rotating Elements:
- deque supports rotation operations, allowing you to rotate the elements to the left or right by a specified number of positions.
- Iterating Efficiently:
- deque provides efficient iteration from both ends using popleft() and pop() for the left end and popright() and append() for the right end. This is useful when you need to process elements in a specific order.
- Memory Efficient:
- While list objects in Python have dynamic array implementations, deque objects use a doubly-linked list, which makes them more memory-efficient for scenarios involving frequent insertions and deletions.
- Thread-Safe Operations:
- The deque data structure supports atomic operations, making it safe to use in multi-threaded programs without additional locking.
from collections import deque
# Create a deque with a maximum length of 3
d = deque(maxlen=3)
d.append(1)
d.append(2)
d.append(3)
d.append(4) # This will remove 1 from the other end to maintain the maximum length
from collections import deque d = deque([1, 2, 3, 4, 5]) d.rotate(2) # Rotate elements to the right by 2 positions
In summary, deque is a versatile and efficient data structure for managing collections of items where you need fast insertions and removals from both ends. It's a valuable tool for building various data structures and solving a wide range of programming problems efficiently.
Example:
Objective:
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.
Example:
>>> from collections import deque
>>> d = deque()
>>> d.append(1)
>>> print d
deque([1])
>>> d.appendleft(2)
>>> print d
deque([2, 1])
>>> d.clear()
>>> print d
deque([])
>>> d.extend('1')
>>> print d
deque(['1'])
>>> d.extendleft('234')
>>> print d
deque(['4', '3', '2', '1'])
>>> d.count('1')
1
>>> d.pop()
'1'
>>> print d
deque(['4', '3', '2'])
>>> d.popleft()
'4'
>>> print d
deque(['3', '2'])
>>> d.extend('7896')
>>> print d
deque(['3', '2', '7', '8', '9', '6'])
>>> d.remove('2')
>>> print d
deque(['3', '7', '8', '9', '6'])
>>> d.reverse()
>>> print d
deque(['6', '9', '8', '7', '3'])
>>> d.rotate(3)
>>> print d
deque(['8', '7', '3', '6', '9'])Task
Perform append, pop, popleft and appendleft methods on an empty deque d.
Input Format
The first line contains an integer N, the
number of operations.
The next N lines contains the space separated names of
methods and their values.
Constraints
- 0 < N <= 100
Output Format
Print the space separated elements of deque d.
Sample Input
6
append 1
append 2
append 3
appendleft 4
pop
popleftSample Output
1 2Solution – Collections.deque() in Python
# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import deque
D = deque()
for _ in range(int(input())):
oper, val, *args = input().split() + ['']
eval(f'D.{oper} ({val})')
print(*D)
is Provided by CodeCrazypy. This tutorial is only for Educational and Learning Purpose.
