Using .append() With Deques in Python
00:00
In this lesson, you’ll see the append methods that work for double-ended queues. The double-ended queue structure is a class from the collections
module.
00:11
It allows for enqueuing and dequeuing off either end of the queue. It’s pronounced “deck.” Calling it “de-queue” would create some confusion with the operation dequeue, so you say “deck.” You can perform the operations using .pop()
and .append()
to dequeue and enqueue off the right-hand side and .popleft()
and .appendleft()
to dequeue and enqueue off the left-hand side.
00:39 Its initializer takes two optional arguments: an initial queue and the maximum size of the queue, should you want one. Knowing how to create one and what its operations are, using it is like you would expect. First, import the class.
01:14 with initial values. Notice, we’re back to using a more general type of Python collection, which can allow multiple types of data. You can append to it on either end.
01:34
There, it enqueued 'b'
on the right.
01:46
And this enqueued -1.0
on the left. And since it’s double ended, you can pop off either end as well. First, dequeuing off the right, and then off the left.
02:07
Remember, the dequeue operation returns the item that was just popped off of the queue. And there’s using the deque
class’s two append operations, .append()
and .appendleft()
.
02:21 And that’s the course! Next, you’ll review everything you’ve done.
Become a Member to join the conversation.