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 :



Quiz Level 1

Quiz Level 2


Request Me

 

Comments

Popular posts from this blog

How To Create .ENV File in Python

Create a Large file for Test Data Processing using Python

How to solve the Liner Equation using Python