Code Explanation:
1. from collections import deque
Imports the deque class from Python’s collections module.
deque stands for double-ended queue — it allows fast appending and popping from both ends (left and right).
2. dq = deque([10, 20, 30])
Creates a deque (like a list) with three elements:
dq = deque([10, 20, 30])
Current state:
[10, 20, 30]
3. dq.append(40)
Adds (appends) 40 to the right end of the deque.
New state:
[10, 20, 30, 40]
4. dq.appendleft(5)
Adds 5 to the left end of the deque.
New state:
[5, 10, 20, 30, 40]
5. dq.pop()
Removes and returns the rightmost element (40).
New state:
[5, 10, 20, 30]
6. dq.popleft()
Removes and returns the leftmost element (5).
New state:
[10, 20, 30]
7. print(list(dq))
Converts the deque to a list for printing.
Final output:
[10, 20, 30]
Final Output:
[10, 20, 30]
.png)

0 Comments:
Post a Comment