I corrected a few problems below. Also, a 'stack', in abstract programming terms, is usually a collection where you add and remove from the top, but the way you implemented it, you're adding to the top and removing from the bottom, which makes it a queue.

class myStack:
     def __init__(self):
         self.container = []  # You don't want to assign [] to self - when you do that, you're just assigning to a new local variable called `self`.  You want your stack to *have* a list, not *be* a list.

     def isEmpty(self):
         return self.size() == 0   # While there's nothing wrong with self.container == [], there is a builtin function for that purpose, so we may as well use it.  And while we're at it, it's often nice to use your own internal functions, so behavior is more consistent.

     def push(self, item):
         self.container.append(item)  # appending to the *container*, not the instance itself.

     def pop(self):
         return self.container.pop()  # pop from the container, this was fixed from the old version which was wrong

     def peek(self):
         if self.isEmpty():
             raise Exception("Stack empty!")
         return self.container[-1]  # View element at top of the stack

     def size(self):
         return len(self.container)  # length of the container

     def show(self):
         return self.container  # display the entire stack as list


s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print(s.show())
Answer from Brionius on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_dsa_stacks.asp
Stacks with Python
So when removing a pancake, it will always be the last pancake you added. This way of organizing elements is called LIFO: Last In First Out. ... Push: Adds a new element on the stack.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ stack-in-python
Stack in Python - GeeksforGeeks
DSA Python ยท Data Science ยท NumPy ... Last-In/First-Out (LIFO) principle. The last inserted element is removed first ยท Insertion operation is called push ยท...
Published: May 19, 2026
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to use the stack()?
r/learnpython on Reddit: how to use the Stack()?
October 26, 2022 -

I'm just wondering how I can use the Stack function alongside pop() and push() and peak() ? Is it a module that i have to import?

Stack usage - Python Sep 17, 2022
r/learnpython
4y ago
Solving the stack problem Feb 5, 2022
r/ProgrammingLanguages
4y ago
I don't understand how Python 3 uses stacks in recursion. Feb 27, 2025
r/learnprogramming
last yr.
Stacks and Queues, when to use them? Feb 9, 2018
r/learnpython
8y ago
Is there a way to find the length of a stack? Oct 29, 2022
r/learnpython
3y ago
More results from reddit.com
Top answer
1 of 12
16

I corrected a few problems below. Also, a 'stack', in abstract programming terms, is usually a collection where you add and remove from the top, but the way you implemented it, you're adding to the top and removing from the bottom, which makes it a queue.

class myStack:
     def __init__(self):
         self.container = []  # You don't want to assign [] to self - when you do that, you're just assigning to a new local variable called `self`.  You want your stack to *have* a list, not *be* a list.

     def isEmpty(self):
         return self.size() == 0   # While there's nothing wrong with self.container == [], there is a builtin function for that purpose, so we may as well use it.  And while we're at it, it's often nice to use your own internal functions, so behavior is more consistent.

     def push(self, item):
         self.container.append(item)  # appending to the *container*, not the instance itself.

     def pop(self):
         return self.container.pop()  # pop from the container, this was fixed from the old version which was wrong

     def peek(self):
         if self.isEmpty():
             raise Exception("Stack empty!")
         return self.container[-1]  # View element at top of the stack

     def size(self):
         return len(self.container)  # length of the container

     def show(self):
         return self.container  # display the entire stack as list


s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print(s.show())
2 of 12
5

Assigning to self won't turn your object into a list (and if it did, the object wouldn't have all your stack methods any more). Assigning to self just changes a local variable. Instead, set an attribute:

def __init__(self):
    self.stack = []

and use the attribute instead of just a bare self:

def push(self, item):
    self.stack.append(item)

Also, if you want a stack, you want pop() rather than pop(0). pop(0) would turn your data structure into a(n inefficient) queue.

๐ŸŒ
Edureka
edureka.co โ€บ blog โ€บ stack-in-python
Stack in Python | What is Python Stack and how to Implement it | Edureka
November 27, 2024 - Pythonโ€™s built-in list type makes a decent stack data structure as it supports push and pop operations in amortized O(1) time.
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ create-a-stack-in-python
Stack in Python: How to Build a Python Stack Data Structure | Codecademy
Learn to implement a stack in Python with our step-by-step guide. Build a custom Python stack data structure using classes, `.push()`, `.pop()`, and `.peek()` methods.
๐ŸŒ
Medium
medium.com โ€บ @dillawarnwl โ€บ exploring-stacks-in-python-a-powerful-data-structure-for-efficient-operations-45373ea16f44
Exploring Stacks in Python: A Powerful Data Structure for Efficient Operations | by Mohammad Dillawar | Medium
May 23, 2023 - โ€” The elements are added and removed from the end of the list, which represents the top of the stack. โ€” The append() method is used to push elements onto the stack, and the pop() method is used to remove elements from the stack.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ oop โ€บ python-oop-exercise-6.php
Python OOP: Stack class with push and pop methods
July 9, 2025 - It has an attribute called items, which is initially an empty list. The "push()" method takes an item as an argument and appends it to the items list, effectively adding it to the top of the stack.
Find elsewhere
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ implementing python stack: functions, methods, examples & more
Implementing Python Stack: Functions, Methods, Examples & More - Analytics Vidhya
June 10, 2025 - Stacks in Python, like in many ... structure. Letโ€™s explore Python stack methods: push(item): This method adds an element (item) to the top of the stack....
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ oop โ€บ python-oop-exercise-9.php
Python OOP: Stack class with push, pop, and display methods
July 9, 2025 - It has an attribute called items, which is initially an empty list. The "push()" method takes an item as an argument and appends it to the items list, effectively adding it to the top of the stack.
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ advanced-built-in-data-structures-and-their-usage โ€บ lessons โ€บ exploring-stacks-with-python-lists
Exploring Stacks with Python Lists
For the Push operation, we use append(), which adds an element at the list's end. For the Pop operation, there's the pop() function that removes the last element, simulating the removal of the 'top' element in a stack. Here's how it looks: stack = [] # A new empty stack # Push operations ...
๐ŸŒ
Real Python
realpython.com โ€บ how-to-implement-python-stack
How to Implement a Python Stack โ€“ Real Python
March 18, 2026 - Youโ€™ll look at the following Python stack implementations: ... The built-in list structure that you likely use frequently in your programs can be used as a stack. Instead of .push(), you can use .append() to add new elements to the top of your stack, while .pop() removes the elements in the ...
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ python-stack-data-structure-when-and-why-to-use-it
Python Stack Data Structure: When and Why to Use it | Codecademy
Push: This operation adds a new item to the top of the stack. Pop: This operation removes the item from the top of the stack. Check out the Create a Stack in Python article to learn how to implement these operations in Python.
๐ŸŒ
Great Learning
mygreatlearning.com โ€บ blog โ€บ it/software development โ€บ stack in python: how to implement python stack?
Stack in Python: How To Implement Python Stack?
October 24, 2024 - The most basic methods associated with a Stack in python are as follows: push(n)โ€“ This is a user-defined stack method used for inserting an element into the stack.
๐ŸŒ
Programiz
programiz.com โ€บ dsa โ€บ stack
Stack Data Structure and Implementation in Python, Java and C/C++
Before pushing, we check if the stack is already full ยท Before popping, we check if the stack is already empty ... Stack Visualization: Don't just read about stack, watch it happen live. See how each line of the data structure works step-by-step with our new DSA visualizer. Try it yourself! The most common stack implementation is using arrays, but it can also be implemented using lists. ... # Stack implementation in python # Creating a stack def create_stack(): stack = [] return stack # Creating an empty stack def check_empty(stack): return len(stack) == 0 # Adding items into the stack def pu
๐ŸŒ
Python.org
discuss.python.org โ€บ documentation
Dis.py: STACK.append vs STACK.push - Python Discussions
February 8, 2024 - I need to practice my backport cherry picking (and corresponding pull requesting). I have a simple case in mind. Stack operation activity described in the file library/dis.rst mentions both STACK.append and STACK.push as the inverse of STACK.pop. Iโ€™m old-fashioned and prefer push to append, but if the concensus is to use append, Iโ€™ll change push to append.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ stack in python
Stack in Python - Scaler Topics
May 22, 2023 - The functions/methods associated ... top() โ€“ Returns a reference to the topmost available element stored in the stack ยท push(a) โ€“ Inserts an element at the top of the stack...
๐ŸŒ
Built In
builtin.com โ€บ articles โ€บ stack-in-python
A Complete Guide to Stacks in Python | Built In
April 9, 2025 - To work with stacks in Python, you should be familiar with the following functions: push(): Adds an element to the stack.
๐ŸŒ
Scout APM
scoutapm.com โ€บ home โ€บ blog โ€บ how to create a python stack
How to Create a Python Stack | Scout Monitoring
July 9, 2020 - The element pushed first takes the bottom-most (last from the top) position as we stack more elements. The second element to be added takes the second-last position from the top, and so on and so forth. Below is a representation of the stack and the corresponding push operations.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python_data_structure โ€บ python_stack.htm
Python - Stack
Such feature is known as Last in First Out(LIFO) feature. The operations of adding and removing the elements is known as PUSH and POP. In the following program we implement it as add and and remove functions.