FAQ: Queues: Python - Queues Python Review

This community-built FAQ covers the “Queues Python Review” exercise from the lesson “Queues: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

Linear Data Structures

FAQs on the exercise Queues Python Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello, when creating the dequeue method, we get instructed to return the value of the head at the end. Why are we returning the value of the head? If I remove return item_to_remove.get_value() at the end of the method, it does not make a difference when printing all the deli examples in the review section. Thanks in advance for the help :slightly_smiling_face:

In a real queue type the main goal is probably not to print whatever you’re removing to the console, it’s much more likely you want to get that item back from the queue. So for that specific example where your only goal is to print the explicit print statement works but for a data structure that has more uses you’d probably want to actually return the object. You can still print what you return if you like but you can also make actual use of the returned object.

1 Like

Thank you so much for taking the time to respond. Ok, so that makes sense. However, it presents a new question. Why are we returning the value of the head we just removed (item_to_remove) rather than the value of the next node, which becomes the new head (given that size > 1) or None (given that size == 1)?

1 Like

The queue they’re building is a first in, first out structure.

Say we stuck three items in this queue structure, the integers 1, 2 and 3 in that order.

The queue would then have nodes containing 1 (first-in) at the head of the queue followed by nodes containing 2 and then 3 such that the nodes are linked like, 123 and we keep a reference to the Queue object.

To get first out the head node is accessed from the Queue object which is the node containing integer 1. So by removing the head node we get the first item put in as the first item out (and then move the head node to the next object by order of insertion or None). Jumping forwards would mess up the order here.

1 Like

It’s not fully clicking and I think is because I don’t have any experience at all. The way I am seeing it is that if there is only one item in the queue, the head and tail become None. If there is more than one item in the queue we move the head to the next element of the current head and decrease the size of the queue by one (since at that point we have removed the first item in, which is the head). Everything makes sense up to the item_to_remove.get_value() part.

What happens if we do not return item_to_remove.get_value() and use the method? For instance if we had that same queue containing 123 and used the method without returning item_to_remove.get_value(), will it still remove the head (1)?

I really appreciate your patience with this, Tim :slightly_smiling_face:

1 Like

A lot of these things takes a while to sink in and it’ll probably seem a bit of an uphill struggle. However, if you went back to the start of the data structures topic now you’d probably find it significantly easier than the first time around (you will probably also find Python’s classes easier to work with as well). It just takes time and practice like any skill, the more you play around with and test it out the more practice you’ll get.

If there was a single item in the queue both .head and .tail should refer to the same object rather than None. If there were no items in the queue then .head and .tail would refer to None.

If you removed return from that method then yes it would still remove a node at the front of your queue.

If you’ve come across things like the list type’s .pop method it’s roughly the same operation where you remove an item from the list and return that item. Arguably if you used .append and .pop then the list acts like a last-in, first-out queue (stack behaviour) but the idea of removing items from the container and giving those items back to the caller is the same.

So you could write a method that removes items from your FIFO queue without returning them, it’s just less useful; after all you can still use .pop and ignore the returned object anyway.

All return does is pass a reference to that object back to the caller-

# If the removed item is returned...
result = deli_line.dequeue()
print(result)
# you can also remove the current head and ignore it...
deli_line.deque()

Without the return you can remove items (as a side effect you alter your list) but that would be all.

1 Like

Thank you so much for your encouraging words, Tim. I am very patient when it comes to learning things that I truly like but sometimes I worry I might frustrate other people with questions that might be very obvious to others, you know?

Amazing!! I see that you stored the removed item in a variable, which at that point the removed item can be accessed (if needed). That makes so much sense. Return (in this scenario) is basically like saying “This is the item that was removed after performing the steps above” but that’s it, correct? Like letting the function caller know.

It’s all good, any replies are voluntary :slightly_smiling_face:. I may be misinterpreting you but it’s a bit more than just letting the caller know; you are genuinely handing a reference to this object back to the caller. This would normally be used immediately by assigning this reference to a new name in the same scope as the caller or using it in an expression.

The whole point of the Queue is to literally queue things up. In a real world example this could be orders in a restaurant so that if order#273 is made before #274 then #273 is served (returned from) before #274. In a more programming related queue you might be temporarily queueing user input like keypresses so that all operations will be completed in the order they were performed (when there’s a reason they cannot be done immediately). There’s all sorts you can do with buffering, asynchronous operations and more that a simple structure like a Queue can help with; Python has a whole module for queues because they’re genuinely useful. Bear in mind there’s no real restriction on what type goes in your queue, it could be a very complex object if you needed it to be. If there was no return then there would be no point to putting things in the Queue in the first place. The Queue is for enforcing a queue, it’s more like a buffer than a normal container; it’s only worth adding items that you want back in a very specific order (FIFO here).

1 Like

Perfect! Bookmarking all of this :smiley:

Thank you so much for your help and patience! Up next, stacks :slightly_smiling_face:

1 Like

I’m currently working on the introductory queues module in the CS track and I’ve reached the topic of bounded queues.

  1. What benefits does limiting the size of a queue serve?

  2. What are some real-world applications of this data structure?