Deque in Python
Concept
Deque (Doubly Ended Queue) in python is implemented using the module “collections“. Deque is preferred over the list in the cases where we need quicker append and pop operations from both the ends of the container, as deque provides an O(1) time complexity for append and pop operations as compared to list which provides O(n) time complexity.
Code
import collections
dq = collections.deque([20, 30, 40])
print(dq)
# added in right end
dq.append(50)
print(dq)
# added in left end
dq.appendleft(6)
print(dq)
# deleteing from right end
dq.pop()
# deleting from left end
dq.popleft()
Screenshot :
Comments
Post a Comment