Editor

Python Program to Implement a Stack Using a List

Intermediate · Data structures · CBSE class 12 practical list

A stack is a data structure where the last item added is the first one removed (LIFO, last in first out), like a pile of plates. Adding an item is called push, removing the top item is called pop, and looking at the top item without removing it is called peek. This program builds a stack on a Python list and handles popping from an empty stack, which is called underflow.

Program

stack_using_list.py
stack = []
def push(item):
stack.append(item) # the end of the list is the top of the stack
print("Pushed", item)
def pop():
if len(stack) == 0:
print("Underflow: the stack is empty, nothing to pop")
else:
print("Popped", stack.pop())
def peek():
if len(stack) == 0:
print("The stack is empty, there is no top element")
else:
print("Top element:", stack[-1])
def display():
if len(stack) == 0:
print("The stack is empty")
else:
print("Stack from top to bottom:", stack[::-1])
# A menu would call these after: choice = int(input("1.Push 2.Pop 3.Peek 4.Display: "))
push(10)
push(20)
push(30)
display()
peek()
pop()
pop()
pop()
pop() # the stack is empty now, so this is an underflow
display()
Output
Pushed 10
Pushed 20
Pushed 30
Stack from top to bottom: [30, 20, 10]
Top element: 30
Popped 30
Popped 20
Popped 10
Underflow: the stack is empty, nothing to pop
The stack is empty

How it works

  1. The list stack holds the items, and its last element is treated as the top of the stack.
  2. push() adds an item on top with append(), and pop() uses the list method pop(), which removes and returns the last item.
  3. Before popping or peeking, the functions check len(stack) == 0, because popping an empty list would crash with an IndexError; printing an underflow message instead is what the question asks for.
  4. peek() reads the top item with stack[-1] without removing it.
  5. display() prints stack[::-1], the list reversed, so the top of the stack comes first.

Another way to write it

A board exam style question: push only the multiples of 3

A board exam style question: push only the multiples of 3
def push_multiples_of_3(numbers, stack):
for n in numbers:
if n % 3 == 0:
stack.append(n)
def pop_all(stack):
while len(stack) > 0:
print("Popped", stack.pop())
print("Stack is empty")
stack = []
push_multiples_of_3([12, 7, 9, 20, 33, 4], stack)
print("Stack after pushing:", stack)
pop_all(stack)
Output
Stack after pushing: [12, 9, 33]
Popped 33
Popped 9
Popped 12
Stack is empty

Board papers often ask for two functions like these: one pushes only the items that pass a test, and the other pops and prints until the stack is empty. The items come out in reverse order: 33, 9, 12.

Try it yourself

Give the stack a size limit of 3 and make push() print "Overflow" when the stack is already full.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises