Python Double Ended Queue or DeQueue implementation. We can insert and access data at the beginning as well as at the end of a double-ended queue. The following code shows how to implement a double-ended queue data structure in the Python programming language.
Code:
import collections
double_ended_queue = collections.deque(["Mon", "Tue", "Wed"])
double_ended_queue.append("Thu")
print("Appended at right - ")
print(double_ended_queue)
double_ended_queue.appendleft("Sun")
print("Appended at right at left is - ")
print(double_ended_queue)
double_ended_queue.pop()
print("Deleting from right - ")
print(double_ended_queue)
double_ended_queue.popleft()
print("Deleting from left - ")
print(double_ended_queue)
Output:
Appended at right -
deque(['Mon', 'Tue', 'Wed', 'Thu'])
Appended at right at left is -
deque(['Sun', 'Mon', 'Tue', 'Wed', 'Thu'])
Deleting from right -
deque(['Sun', 'Mon', 'Tue', 'Wed'])
Deleting from left -
deque(['Mon', 'Tue', 'Wed'])