Code Explanation:
1) from collections import deque
Imports the deque (double-ended queue) class from Python’s collections module.
deque allows fast appends and pops from both ends (left and right).
2) dq = deque([1, 2, 3])
Creates a new deque initialized with the list [1, 2, 3].
So, dq starts as:
deque([1, 2, 3])
3) dq.append(4)
Adds 4 to the right end of the deque.
Now dq is:
deque([1, 2, 3, 4])
4) dq.appendleft(0)
Adds 0 to the left end of the deque.
Now dq is:
deque([0, 1, 2, 3, 4])
5) dq.pop()
Removes and returns the rightmost element (4) from the deque.
After popping:
deque([0, 1, 2, 3])
6) dq.popleft()
Removes and returns the leftmost element (0) from the deque.
After popleft:
deque([1, 2, 3])
7) print(list(dq))
Converts the deque into a normal Python list and prints it.
Output:
[1, 2, 3]
Final Output:
[1, 2, 3]
.png)

0 Comments:
Post a Comment