Skip to content
Computer ScienceCBSE Class 12 · Stack

Push and Pop Operations on a Stack Using a Python List (Class 12 Computer Science)

Updated 17 October 2026 · Fact-checked

In Python a stack is a list whose last element is the top. Push is stack.append(item). Pop is stack.pop() after checking that the list is not empty, because pop() on an empty list raises IndexError; printing "Underflow" for an empty stack is the standard board answer.

Push and pop both happen only at the top, the last index of the list; popping an empty stack gives Underflow.

Illustration drawn for QuestionBuddy.

Understand Push and Pop Operations on a Stack Using a List

A Python list is the stack. Keep the bottom at index 0 and the top at the last index, so adding and removing both happen at the end, which Python does quickly.

The four small operations.

  • Push: stack.append(item) adds at the top.
  • Pop: stack.pop() removes the last element and returns it.
  • Peek: stack[-1] reads the top without removing it.
  • isEmpty: len(stack) == 0 (or stack == []) is True when there is nothing in the stack.

Underflow. Calling pop() on an empty list raises IndexError: pop from empty list. A correct function checks first and reports "Underflow" (or returns None). Overflow needs a fixed size limit; a Python list has none, so a board program that mentions a limit adds its own check, for example if len(stack) == 5.

Standard functions, as the board writes them.

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

def pop(stack):
    if stack == []:
        print("Underflow")
        return None
    return stack.pop()

def peek(stack):
    if stack == []:
        return None
    return stack[-1]

def display(stack):
    for i in range(len(stack) - 1, -1, -1):
        print(stack[i])

display prints from the top down, because that is the order in which the items would be popped. push needs no global statement because append changes the list in place and does not assign a new object to the name.

Typical call pattern. Create st = [] once, call push(st, 10), push(st, 20), then pop(st) returns 20. Always keep one stack object and pass it to every function.

Writing the conditional push and "pop all" loop is the next topic; this page fixes the individual operations.

Push and Pop Operations on a Stack Using a List: key facts

Push and pop in Python
Pushstack.append(item)
Popstack.pop()
Peekstack[-1]
Empty teststack == [] or len(stack) == 0
The top is the last element of the list.
Underflow
  • pop on an empty stack
  • Python raises IndexError: pop from empty list
  • Check before popping and print Underflow
A board function returns None after printing Underflow.
Overflow
  • Only for a stack with a size limit
  • Test len(stack) == limit before append
  • A plain list never overflows
State the limit in the question before coding it.
Return value of pop
  • pop() removes the item and returns it
  • Capture it: x = stack.pop()
  • Without capturing, the value is lost
Display functions must not pop unless asked.

How to answer Push and Pop Operations on a Stack Using a List questions

Use this routine for any "write a function" question on stack operations.

  1. 1Name the stack list as a parameter of every function, so the same list is shared by push, pop and display.
  2. 2Push: one line, append(item). Add a size test only if the question gives a limit.
  3. 3Pop: test for empty first; if empty print "Underflow" and return None; otherwise return stack.pop().
  4. 4Display: loop from the last index to 0 so the top prints first, or loop over a reversed copy.
  5. 5Do not use insert(0, ...) or pop(0): that works on the other end and breaks LIFO thinking.
  6. 6Finish by writing a short call sequence and the exact output.

Push and Pop Operations on a Stack Using a List in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Undo history of a note app

Suppose a note app keeps a list of typed words in the list history, with the newest word last. Undo should remove and show the latest word.

history = ["buy", "milk", "today"]. Write the output of pop(history) twice using the standard pop function.

Try it first, then show the solutionSolution
  1. The top is the last element: "today".
  2. First call returns "today", leaving ["buy", "milk"]. Second returns "milk", leaving ["buy"].

Answer: today then milk; history is now ['buy']

Memory hook: Undo takes the word typed last, never the first.

Empty cart removal

Suppose a shop app lets the user remove the last item added to a cart, even when the cart might already be empty.

What should the app do when it calls pop on an empty cart?

Try it first, then show the solutionSolution
  1. pop on an empty list raises IndexError.
  2. The function tests for empty first, prints Underflow and returns None.

Answer: It prints Underflow instead of crashing.

Memory hook: Look before you lift: check the pile is not empty.

Tricks to remember Push and Pop Operations on a Stack Using a List

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • A-P-P-L-E: append is push

    append = push, pop = pop, [-1] = peek.

    Use it when: Mapping stack operations to list code.

  • Check before you pop

    Ask "is there anything on the pile?" before taking from it.

    Use it when: Preventing IndexError and underflow.

  • Display from the top

    The first thing shown should be the first thing that would be popped.

    Use it when: Writing display with range(len-1, -1, -1).

  • pop() returns, [-1] only looks

    pop takes the top away; stack[-1] just looks at it.

    Use it when: Peek versus pop questions.

Push and Pop Operations on a Stack Using a List: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: List method for push · 1 mark · MCQBoard level

A list named box serves as a stack with its top at the right end. Which call pushes the value 9?
(a) box.insert(0, 9) (b) box.add(9) (c) box.append(9) (d) box.extend(9)

Try it first, then show the solutionSolution
  1. The top is the last element, so the item is added at the end.
  2. append() adds at the end.

Answer: (c) append()

Example 2: Trace push and pop · 2 marks · Short answerBoard level

Write the output:

st = []
st.append(4)
st.append(7)
st.append(1)
print(st.pop(), st[-1], len(st))
Try it first, then show the solutionSolution
  1. After three appends: [4, 7, 1].
  2. The arguments are evaluated left to right: st.pop() removes and gives 1; the list is now [4, 7]; st[-1] is 7; len(st) is 2.

Answer: 1 7 2

Example 3: Pop with underflow check · 3 marks · Short answerHarder board level

Write a function pop(stack) that removes and returns the top item of a stack kept in a list, and prints Underflow when the stack is empty. Show the output for st = [3] after calling pop(st) twice.

Try it first, then show the solutionSolution
  1. def pop(stack):
        if stack == []:
            print("Underflow")
            return None
        return stack.pop()
    (2 marks for the check and the return)
  2. First call returns 3 and leaves []. (0.5 mark)
  3. Second call prints Underflow and returns None. (0.5 mark)

Answer: The first call returns 3; the second prints Underflow.

Example 4: Stack with a size limit · 3 marks · Short answerHarder board level

A stack can hold at most 3 items. Write push(stack, item) that prints Overflow when the stack is full. State what is printed and what the stack holds after pushing 5, 6, 7, 8 into an empty stack.

Try it first, then show the solutionSolution
  1. def push(stack, item):
        if len(stack) == 3:
            print("Overflow")
        else:
            stack.append(item)
    (2 marks)
  2. 5, 6, 7 are pushed: [5, 6, 7]. Pushing 8 finds len 3 and prints Overflow. (1 mark)

Answer: Overflow is printed once; the stack stays [5, 6, 7].

Example 5: Case: ticket counter · 4 marks · Case-basedHOTS / competency

Suppose a ticket-return desk stores returned ticket numbers in a list tk, newest last, and always serves the newest return first. Currently tk = [101, 102, 103].
(i) Write a statement to add ticket 104. (1 mark)
(ii) Write the output of the code below. (2 marks)

tk = [101, 102, 103]
tk.append(104)
x = tk.pop()
y = tk.pop()
tk.append(x + y)
print(tk, tk[-1])

(iii) Name the data structure property used. (1 mark)

Try it first, then show the solutionSolution
  1. (i) tk.append(104)
  2. (ii) After append: [101, 102, 103, 104]. x = 104, y = 103, tk = [101, 102]. x + y = 207; tk = [101, 102, 207]; tk[-1] = 207.
  3. (iii) LIFO: the newest return is served first.

Answer: (i) tk.append(104) (ii) [101, 102, 207] 207 (iii) LIFO

Where students lose marks in Push and Pop Operations on a Stack Using a List

The slips examiners see most often, and what to write instead.

  • Mistake: Using pop(0) as the stack pop

    pop(0) removes the bottom item, which is queue behaviour.

    Wrong working

    return stack.pop(0)

    Right working

    return stack.pop()

    Do this: The top is the last index, so use pop() or pop(-1).

    Usually costs: 1 mark

  • Mistake: No empty check before pop

    On an empty list pop raises IndexError and the program stops.

    Wrong working

    def pop(stack):
    return stack.pop()

    Right working

    def pop(stack):
    if stack == []:
    print("Underflow")
    return None
    return stack.pop()

    Do this: Write the test first; marking schemes award it separately.

    Usually costs: 1 mark

  • Mistake: Printing the stack inside pop

    The question says return the popped item; a print loses it for later use.

    Wrong working

    def pop(stack):
    print(stack.pop())

    Right working

    def pop(stack):
    return stack.pop()

    Do this: Return the value; print it in the calling code.

    Usually costs: 0.5 mark

Exam tips

  • Always take the stack as a parameter, and say so in the first line of the function.
  • Write Underflow exactly as the question spells it, with the same capital letter.
  • Use pop() and append(); avoid insert(0, x) because it pushes at the bottom.
  • If a size is given, test len(stack) against it before append and print Overflow.
  • End with a short call and its output; one sample call earns the last mark.

Practice questions on Push and Pop Operations on a Stack Using a List

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    Which statement reads the top of a list stack st without removing it?
    (a) st.pop() (b) st[0] (c) st[-1] (d) st.top()

    Hint

    Last index is the top.

    Answer

    (c) st[-1]

  2. Q2 · 2 marks · Short answerBoard level

    What error does st.pop() raise when the list st is empty, and what is the condition called?

    Hint

    Two words: the Python error and the stack term.

    Answer

    IndexError (pop from empty list); the condition is underflow.

  3. Q3 · 3 marks · Short answerHarder board level

    Write a function peek(stack) that returns the top item without removing it, or prints "Empty" and returns None when the stack is empty. Show the result of peek on [8, 9, 2] and on [].

    Hint

    Test emptiness first.

    Answer
    def peek(stack):
        if stack == []:
            print("Empty")
            return None
        return stack[-1]

    peek([8, 9, 2]) returns 2 (list unchanged); peek([]) prints Empty and returns None.

  4. Q4 · 3 marks · Short answerHarder board level

    Write the output:

    st = [2, 4]
    for n in range(3):
        st.append(st[-1] + n)
    print(st)
    Hint

    Each step uses the current top.

    Answer

    n = 0: top 4, append 4. n = 1: top 4, append 5. n = 2: top 5, append 7. Output: [2, 4, 4, 5, 7].

  5. Q5 · 2 marks · Short answerHOTS / competency

    A student writes display(stack) as: for x in stack: print(x). The stack is [1, 2, 3] with top 3. (i) What does it print? (ii) Rewrite it so that it prints in pop order without changing the stack.

    Hint

    Pop order starts at the top.

    Answer

    (i) 1, 2, 3 on separate lines (bottom first, not pop order). (ii) for i in range(len(stack) - 1, -1, -1): print(stack[i]) prints 3, 2, 1.

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.Which list method is push?Show answer

    append().

  2. 2.Which list method is pop?Show answer

    pop() with no argument.

  3. 3.What does stack[-1] give?Show answer

    The top item, without removing it.

  4. 4.What does pop() do on an empty list?Show answer

    Raises IndexError.

  5. 5.What is the stack state called when pop is attempted on an empty stack?Show answer

    Underflow.

  6. 6.Which end of the list is the top?Show answer

    The last (highest index) end.

Push and Pop Operations on a Stack Using a List: frequently asked questions

How do you implement a stack using a list in Python?

Keep the top at the end of the list: use list.append(item) to push, list.pop() to pop, list[-1] to peek, and list == [] or len(list) == 0 to test for empty.

What is stack underflow in Python?

It is popping from an empty stack. For a list, pop() raises IndexError, so a safe pop function checks for an empty list first and prints Underflow.

Does a stack made from a Python list ever overflow?

Not by itself, because a list grows as needed. Overflow appears only when the question fixes a maximum size and the program tests for it.

Is peek in the Class 12 syllabus?

The syllabus names push and pop. Peek and isEmpty are small helpers that board questions use, so know them, but the core is push and pop.