Question:

In Python, ________ module needs to be imported for implementing a Double-Ended Queue?

Updated On: May 28, 2025
  • counter
  • collections
  • random
  • numpy
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Approach Solution - 1

In Python, to implement a Double-Ended Queue (Deque), we need to import the collections module. The collections module provides a range of specialized container data types in addition to Python's built-in containers such as lists, dictionaries, and tuples. One of these is deque (double-ended queue), which supports thread-safe, memory-efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.
To use a deque, you can import and initialize it as follows: 

from collections import deque
dq=deque()

With deque, you can perform operations such as append() and appendleft() for adding elements, along with pop() and popleft() for removing them from either end.

Was this answer helpful?
0
0
Hide Solution
collegedunia
Verified By Collegedunia

Approach Solution -2

In Python, the collections module needs to be imported for implementing a Double Ended Queue (Deque).

Additional Context:

  • Collections Module (Option 2):
    • Contains specialized container datatypes
    • Provides deque class for double-ended queues
  • Deque Implementation:
    from collections import deque
    d = deque([1, 2, 3])
    d.appendleft(0)  # Add to front
    d.append(4)      # Add to end
        
  • Key Features:
    • O(1) complexity for append/pop at both ends
    • Thread-safe for appends/pops
    • Max length can be specified
  • Why Other Options Are Incorrect:
    • counter (1): For counting hashable objects
    • random (3): For generating random numbers
    • numpy (4): For numerical computing

Correct Answer: (2) collections.

Was this answer helpful?
0
0

Top Questions on File Handling in Python

View More Questions