Skip to content
Advertisement

What is the difference between deque( [1,2,3] ) and deque.append( [1,2,3] )?

I have noticed that the following incurs an iterable error.

q = deque([1,2,3])
x, y, z = q.popleft()
# TypeError: 'int' object is not iterable

But the code below works which I thought the same operation:

q = deque()
deque.append([1,2,3])
x, y, z = q.popleft()

What is the difference between the two ways above?

Thanks for your help in advance.

Advertisement

Answer

q.popleft() returns the first element of the deque. In the first case it is int and in the second it is a list of ints. You should write in the first case: x, y, z = q.

Advertisement