HandHand
HandHand

Reputation: 45

Time complexity when checking deque is empty

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

Answers (1)

ruohola
ruohola

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

Related Questions