---
title: "Stacks: Last In, First Out"
description: "A collection where the only value within reach is the one added most recently. Push puts a value on top, pop takes it off, and that single restriction is what makes undo, back buttons and bracket chec"
canonical: https://lightmysky.com/learn/computing/stacks-last-in-first-out-mt_Evw7zeJE6Q
source: https://lightmysky.com/learn/computing/stacks-last-in-first-out-mt_Evw7zeJE6Q.md
retrieved: 2026-09-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# Stacks: Last In, First Out

A collection where the only value within reach is the one added most recently. Push puts a value on top, pop takes it off, and that single restriction is what makes undo, back buttons and bracket checking straightforward.

Subject: Computing · Area: Algorithms & Data Structures · Ages 16 to 18
Page: https://lightmysky.com/learn/computing/stacks-last-in-first-out-mt_Evw7zeJE6Q

## Ready when they can

- Trace the contents of a stack through a run of pushes and pops
- Implement push and pop over a list and say what a pop from an empty stack should do
- Match a task such as undo history or bracket matching to the last-in-first-out rule

## Lesson: The pile you only touch on top

A stack is a pile you only touch from the top. Push adds an item on top and pop removes the top item, so the last thing added is always the first thing out. That is the whole rule: last in, first out. Everything else about stacks is just this rule applied carefully.

**Example.** To trace a stack, draw the pile after every single step and never skip one. Push writes the value on top, pop crosses the top value out and hands it back. Try it: push 4, push 7, pop, push 9 leaves 9 on top with 4 beneath. Popping an empty stack is an error, so decide upfront what your program does there: raise an error, return a sentinel, or refuse the operation.

In Python a plain list works as a stack with append as push and pop as pop. Appending puts the value at the end and popping takes it back off, which mirrors the pile exactly. Indexing from the end with minus one lets you peek at the top item without removing it. Peek, compare, and only pop when they match: that rhythm keeps bracket checking honest.

**Tip.** Real programs lean on stacks wherever history must unwind in reverse. An editor undo stack pushes each change and pops it when you press undo, so the newest change vanishes first. A bracket checker pushes opening brackets and pops one for each closing bracket, and any mismatch or leftover means the nesting is broken. When a task says most recent first, reach for a stack.

**Recap.** Last in, first out: push on top, pop from top.

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [Lists: Many Values Under One Name](https://lightmysky.com/learn/computing/lists-many-values-under-one-name-mt_ipB6kmyK85)

## Opens up

- [Queues: First In, First Out](https://lightmysky.com/learn/computing/queues-first-in-first-out-mt_4U1bT0JW4g)
- [Recursion and the Call Stack](https://lightmysky.com/learn/computing/recursion-and-the-call-stack-mt_VGjfeF3F2e)
