FAQ: Stacks: Python - Stacks Python Push and Pop

This community-built FAQ covers the “Stacks Python Push and Pop” exercise from the lesson “Stacks: Python”.

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

Computer Science

Linear Data Structures

FAQs on the exercise Stacks Python Push and Pop

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!

Hi, I need some help to understand why:
self.top_item = item
is not equal to:
item = self.top_item

Thank you for your help!

item is a parameter, whereas self.top_item is an attribute. Attributes depend upon an owner context. Parameters are values with no context other than their position in an argument list.

= is not an “equals sign”. It is an assignment operator.
When it is encountered, Python

  1. Obtains a value by evaluating the expression on the right side of =
  2. Assigns that value to the variable on the left side of =

So, do you want to assign the value of item to the variable self.top_item,
… or do you want to assign the value of self.top_item to the variable item?

1 Like

Thank you all (@mtf, @patrickd314) for your answers. Think I got it now.

1 Like

Why is it necessary to have a set_next_node() function when I can simply use

item.next_node = node

to change the value of the next connecting node?

why should i use “item = Node(value)” instead of “item = Node(self.value)” ?
what is the difference ?

It is no help to us if there is no context. You leave us guessing. Please post the URL of this exercise page.

Node is suggestive of a class object. Any instance of that would require a self in references to its variables. self.value seems more apropos.

Sorry for that . The URL :https://www.codecademy.com/paths/computer-science/tracks/linear-data-structures/modules/cspath-stacks/lessons/learn-stacks-python/exercises/stacks-python-push-and-pop
In the Push method definition
def push(self,value):
item = Node(value)
self.top_item = item
why should i use here a “item = Node(value)” instead of “item = Node(self.value)”
thank you

1 Like

Let me look at this tomorrow. I’ve not taken this course and will need to get up to speed to be able to help. With any luck, someone might pick up the slack in the meantime.

Let me try to explain what my intuition is:

As you are method .push() takes an input of “value”, you are not using an original self.value from the class Stack, but need to input a new value to actually be able to call this method.

If you try to call the method .push() without giving it any input, it won’t work. The “value” input you give to the .push() method is what you want to put on top of your stack. This is thus not linked to your self.value.

1 Like

I was just trying to figure out is the head of the linked list removed in the pop function or it stays there as we never really set it to none in the function defined , we only set the head_node to the next_node, while the first node is still pointing to the next node, its really confusing for me trying to figure out whether the value has been removed or not