Reputation: 45
When I want to check whether deque is empty in python, I usually use this code snippet.
from collections import deque
q = deque()
# iterate until deque is empty
while q:
do something...
I wonder how the time complexity is when checking whether the deque is empty. Does it take O(n)?
Is there any faster way to check whether deque is empty?
Upvotes: 1
Views: 360
Reputation: 24155
No there is not. while q
is essentially while len(q)
, and the time complexity of deque.__len__
is O(1).
Upvotes: 1