# Stack (Data Structure)

> A pile where the last item in is the first one out. You push and pop.

**Field:** Software Development  
**Category:** Algorithms & Data Structures  
**Also known as:** Stack, LIFO, Last In, First Out

## Think of it like

Think of it like a pile of plates. You add to the top, and you take from the top.

## Definition

A stack is a list where you can only add or remove at one end, called the top. The last item you put in is the first one out, which is called last in, first out. Adding is called push and removing is called pop. The undo button in an editor is a stack of your past changes. A program also keeps a call stack, so it knows where to return after each function.

## Example

Lucy types three words, then presses undo twice. The stack of changes gives back the last word, then the one before it.

## Example code

```python
>>> s = []
>>> s.append(1)
>>> s.append(2)
>>> s.pop()
2
```

2 went in last, so it comes out first.

## Why it matters

Undo, the back button and each function call all use a stack.

## Common mistake

Popping from an empty stack. Check that it has items first, or the program stops with an error.

## Three principles

- **Last in, first out.** Like a pile of plates.
- **Push and pop.** Add and remove at the top.
- **Everywhere.** Undo, back button, function calls.

## Related terms

- [Queue (Data Structure)](https://learn.benibix.com/term/development/queue)
- [Data Structure](https://learn.benibix.com/term/development/data-structure)
- [Linked List](https://learn.benibix.com/term/development/linked-list)
- [Recursion](https://learn.benibix.com/term/development/recursion)

## Told apart from

- [Stack vs Queue](https://learn.benibix.com/compare/stack-vs-queue): A stack and a queue both hold items in a line, but they let them out from different ends. A stack gives back the last item in. A queue gives back the first.

---

Source: https://learn.benibix.com/term/development/stack
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
