Tip: To make the code more efficient, you can use the deque object from the collections module instead of a list, for implementing queue. This way you can use the popleft() method instead of the pop(0) built-in function on queue. This will result in a quicker code as popleft() has a time complexity of O(1) while pop(0) has O(n).
Deque (Doubly Ended Queue) in python is implemented using the module “collections“. Deque is preferred over list in the cases where we need quicker append and pop operations from both the ends of container, as deque provides an O(1) time complexity for append and pop operations as compared to list which provides O(n) time complexity.