The dequeue() method removes and returns the front-most item of the queue. This video demonstrates how to use the Python list's built-in pop() method to mirror this behavior.
- [Narrator] Now that we've coded the enqueue method which helps us get an item into the queue, let's code the dequeue method which will help us get items out of the queue. Now since we are using a list to represent our queue and we are representing the front of the queue with the end of the list. If we use the list's built-in pop method, we'll always get back the front-most item of the queue, because the pop method always returns the last item in the list. So let's go ahead and do that.
We'll return self dot items dot pop. I'll save this and we can go to the terminal and test this out. Let's get the interpreter open. And I'll create a queue object, and let's add something to our queue here. So we'll do my Q dot enqueue, and we'll add the string, apple. Okay, and if we check the my Q dot items attribute, we should get a list that has apple in it, yep, we do.
Now let's dequeue this, let's get apple out of there. So we'll say my Q dot dequeue. We should get apple back to our screen, yep. If we run my Q dot items again, we get an empty list. Let's test the case where we try to dequeue from an empty list. So if I do my Q dot dequeue, I'm probably going to get an error. Yep, I get an index error, and it's saying, basically, that I can't pop an item from a list that has no items.
So we can go back to our code and add a little conditional statement to handle that. We can say if self dot items, meaning if there is something in the items list, then we'll pop something from it. Otherwise, we'll just return none. Let's go back to the terminal and try that out. So I'll have to exit the interpreter and restart this. And recreate my Q object, and straightaway I'm just going to try to dequeue from this, and I should not get an error this time.
It should just return none. And that's what happens, we can double check by saying my Q dot items, it should be an empty list, which it is, great, so we've handled that situation. Now we can go back and add the dock string and our note about the runtime. Let's summarize what this method does. So the dequeue method returns the front-most item, or we should say returns and removes the front-most item from a queue.
Returns and removes the front-most item of the queue, which is represented by the last item in the list. As for the runtime, when we append to or remove from the last item in the list, what we're effectively doing is indexing to the last item of the list, and that happens in constant time, therefore the dequeue method also happens in constant time.
So we'll just say the runtime is of one or a constant time because indexing to the end of a list happens in constant time.
- Abstract types and data structures
- Stacks as a linear abstract data type
- Creating the Stack class and its methods
- Adding items to the top and bottom of a stack
- Creating the Queue class and its methods
- Manipulating items in a queue
- Creating the Dequeue class and its methods
- Adding and removing items from a dequeue