FAQ: Queues: Python - Queues Python Dequeue

This community-built FAQ covers the “Queues Python Dequeue” 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 Dequeue

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!

For the question No.1, inside if statement, check if the size is empty,
I wrote:
if self.size != 0:
However, the answer is
if self.get_size() > 0:
I wrote similar code for question 2
if self.size == 1:
And the official answer for this is:
if self.get_size() == 1:
This didn’t let me pass this assignment, but I was wondering if these two codes works the same.

1 Like

I also tried ‘if not self.is_empty():’ and ‘if self.is_empty() is False:’
The code worked well in my own console but also threw an error in the Codecademy console.

1 Like

The system used to check for correctness seems awfully fickle. During the steps that call for adding print statements, I’ve found that strings formed with double-quotes are required. Writing print('This queue is totally empty!') will error while print("This queue is totally empty!") will pass. This caused me several frustrating minutes of debugging and there is no reason one of those should be deemed invalid.

Additionally, for the step that called for adding the line:
print('Removing ' + str(item_to_remove.get_value()) + ' from the queue!')
I initially wrote this with format strings e.g.
print('Removing %r from the queue!' % item_to_remove.get_value())
because I think they are more readable. These strings should be completely equivalent but the system only allowed the former.


@discreteobject, I don’t know what happened while you working on the project; I copied and pasted both of your examples into the script and it runs fine.

EDIT: I went back and reset the workspace to try to recreate your steps. It would not accept the formatted string using %r, but did accept either %s or the str.format() method. This is not surprising, as %r will include the quotes, if you’re referring to a string (which node.value() is), while %s will not.

my_str = "This is a string"

print("my_str is %r." % my_str)
print("my_str is %s." % my_str)

my_str is 'This is a string'.
my_str is This is a string.

Then I got to the print("This queue is totally empty!") step, which indeed fails when done using single-quotes.

So that part, at least, seems to be a genuine bug. I’ll try to get it reported.

What is the point of all of that? We can do the same using simple lists or dictionnaries…
I guess it is because our Node are very simple object…

I’m stuck at Queues Python Dequeue, point 2. I’ve tried various things and always get a Did you add self as a paramenter for dequeue()?

My current dequeue method is:

  def dequeue(self):
    if not self.is_empty():
      item_to_remove = self.head
      print("Removing " + str(item_to_remove.get_value() + " from the queue!"))
      if self.get_size() == 1:
        self.head = None
        self.tail = None
        self.head = item_to_remove.get_next_node()

What am I doing wrong?

You didn’t close the str in print the right way:


should be: str(item_to_remove.get_value())

Indeed :man_facepalming:. Thanks a lot!

Guys, please clarify for me something. I stuck with understanding. I don’t really understand why do you need to write this line of code to remove the head if the head is more than one - self.head = item_to_remove.get_next_node(). Maybe I don’t understand the concept itself. Form my understanding, the head is the latest node in the queue, so when you set self.head to item_to_remove.get_next_node(), you basically setting to to the next after the head which doesn’t exist.

def dequeue(self):
if not self.is_empty():
item_to_remove = self.head
print(“Removing " + str(item_to_remove.get_value()) + " from the queue!”))
if self.get_size() == 1:
self.head = None
self.tail = None
self.head = item_to_remove.get_next_node()

for this structure makes sense in Stacks where Last in First out is removed, because there is a next item in the stack while in the Queue you remove the front node, so you should set a previous node to the head

Actually, in order to to remove the front node, I would need to check where is the head in the chain of nodes and where is the node that is the one before the head. Once I found the head, I would set the previous node to the head, thus removing the front node. Check this out. I think it makes sense, please critique.

def dequeue(self):
if self.get_size() > 0:
item_to_remove = self.head
current = self.tail
new_node = current.get_next_node()
print(“Removing " + str(item_to_remove.get_value()) + " from the queue!”)
if self.get_size() == 1:
self.head = None
self.tail = None
if new_node == self.head:
self.head = current
while current.get_value():
if new_node ==self.head:
self.head = current
current = None
new_node = new_node.get_next()
current = curent.get_next_node()
return current

1 Like

I am also having a same sort of thinking why cant we set the head node to the node previous to the one that has been dequeued. According to what we did is set it to the next so head node will have None value But how it makes sense.

I don’t understand part of this exercise.

If the queue size is 1 after dequeue, we need to set both head and tail equal to None. Why? When we initially added 1 item to an empty queue, we set the head and tail equal to this node. I would expect we have to set head and tail equal to the same node in the first situation.

This setup also breaks the peek method as there is no head or tail node yet the queue is non-empty. So what’s going on?

I also tried “if self.is_empty == False:” which did not pass, but I switched it to “if self.is_empty != True:” it passed.

In the .dequeue() method we are creating in this exercise, we check to see if there is presently only (1) node in the queue; if there is only one node in the queue (the one we are removing), only then do we set the queue’s tail and head to None:

     if self.get_size() == 1:
        self.head = None
        self.tail = None

I found a couple things confusing with this exercise…

  1. Not sure why the provided solution didn’t use the is_empty() method to check if the queue was empty. Would seem to make sense to take advantage of this method since it was built for this purpose.
  2. Why does the solution require the value of item_to_remove to be returned outside of the dequeue method? return item_to_remove.get_value()

I’m stuck on task 2.
My code:

from node import Node

class Queue:

  def __init__(self, max_size=None):

    self.head = None

    self.tail = None

    self.max_size = max_size

    self.size = 0


  def enqueue(self, value):

    if self.has_space():

      item_to_add = Node(value)

      print("Adding " + str(item_to_add.get_value()) + " to the queue!")

    if self.is_empty():

      self.head = item_to_add

      self.tail = item_to_add



      self.tail = item_to_add

      self.size += 1


      print("Sorry, no more room!")

  # Add your dequeue method below:    

  def dequeue(self):

    if not self.is_empty():

      item_to_remove = self.head

      print(f"Removing {item_to_remove.get_value()} from the queue!")

    elif self.get_size() is 1:

      self.head = None

      self.tail = None


      self.head = self.head.get_next_node()

  def peek(self):

    if self.is_empty():

      print("Nothing to see here!")


      return self.head.get_value()


  def get_size(self):

    return self.size


  def has_space(self):

    if self.max_size == None:

      return True


      return self.max_size > self.get_size()


  def is_empty(self):

    return self.size == 0


q = Queue()

q.enqueue("some guy with a mustache")


Can you help me?

This is the error:

File "queue.py", line 21
SyntaxError: invalid syntax

I’ve finally finished the whole exercise. I just reset the exercise to try again and to get rid of the mistakes. I did it!

print("Exercise completed!")

The task says to check if the queue is empty so I used self.is_empty() but the solution is looking for size > 0?
These statements are equivalent. This is somewhat frustrating since there are multiple answers