To create a Double-Ended Queue (Deque) in Python, import the collections module. This module offers specialized containers beyond Python's standard ones like lists, dictionaries, and tuples. Among these is deque, designed for efficient, thread-safe additions (appends) and removals (pops) from both ends, with near constant O(1) time complexity regardless of the direction.
Initialization of a deque is demonstrated below:
from collections import dequedq=deque()The deque object supports methods such as append() and appendleft() for inserting elements at the right and left ends respectively, and pop() and popleft() for removing elements from the right and left ends.
| 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 |