Deque in Python: Collections Module in Python, Deque.

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:

Python Deque
Deque in Python

  1. 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.
  2. 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.
  3. 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.
      
    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
    
      
      
  4. Rotating Elements:
    • deque supports rotation operations, allowing you to rotate the elements to the left or right by a specified number of positions.
      
    from collections import deque
    
    d = deque([1, 2, 3, 4, 5])
    d.rotate(2)  # Rotate elements to the right by 2 positions
    
    
  5. 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.
  6. 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.
  7. Thread-Safe Operations:
    • The deque data structure supports atomic operations, making it safe to use in multi-threaded programs without additional locking.

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:

    Collections.deque()

                          deque is a double-ended queue. It can be used to add or remove  elements from both ends.

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 appendpoppopleft 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
popleft

Sample Output

1 2

Solution – 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)

Disclaimer: The above Problem (Collections.deque()) is generated by Hacker Rank but the Solution
is Provided by CodeCrazypy. This tutorial is only for Educational and Learning Purpose.

Post a Comment

Previous Post Next Post