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.
In Python, the collections module needs to be imported for implementing a Double Ended Queue (Deque).
Additional Context:
deque
class for double-ended queuesfrom collections import deque d = deque([1, 2, 3]) d.appendleft(0) # Add to front d.append(4) # Add to end
Correct Answer: (2) collections.
List-I | List-II |
(A) readline() | (I) Writes a sequence of strings to the file |
(B) writelines() | (II) Reads a single line from the file |
(C) seek() | (III) Force any buffered output to be written to the file |
(D) flush() | (IV) Moves the file pointer to the specified position |