Stack - Data Structure in Python
Why Are Data Structures Important?
Data structures help us decide how data should be arranged in memory so that a program can work with it efficiently.
Here's what you need to know:
- Data structures are the foundation of computer science. They help you write programs that are fast, efficient, and organized—no matter which programming language you're using.
- A data structure is a way to group and organize data (sometimes of different types) so that it can be handled as one single unit.
- Every data structure has a set of well-defined operations (like insert, delete, search), a particular behavior, and some unique properties.
Data Type VS Data Structure
What is a Data Type?
A data type defines what kind of data a variable can hold. It tells the computer what type of value is stored and how much memory is needed.
Examples:
int– stores whole numbers like 10, -5float– stores decimal numbers like 3.14str– stores text like "Hello"bool– stores either True or False
Think of data types as basic building blocks. They help define the type and nature of a single piece of data.
What is a Data Structure?
A data structure is a way of organizing and storing a group of data so it can be used efficiently. It's like a container that holds multiple values, often of the same or different data types.
Examples:
List– stores a collection of values like [1, 2, 3]Tuple– stores fixed values like (10, 20)Dictionary– stores key-value pairs like {"name": "John"}Stack, Queue, Tree– special ways to organize data for faster operations
Think of data structures as containers or folders that store multiple values together in a specific format.
What is a Stack?
The logical or mathematical model of a particular organization of data is called a data structure. It is a way of storing and accessing data.
A stack is a data structure that follows the LIFO (Last In First Out) order. Think of a scenario where at a dinner party there is a stack of plates - plates are always added or removed from the top of the pile.
- LIFO: The last element added is the first one to be removed.
- List: An array or list is the collection of elements in an ordered way.
Implementation of Stack Using List
The implementation of stack using list in Python is the easiest of all programming languages.
Basic operations performed on stack are:
- Creating a stack
- Push/Adding elements to the stack
- Checking for empty stack
- Pop/Deleting elements from a stack
- Traversal/Displaying a stack
List Methods Used and Important Things to Remember
list.append(element)– Used to implement push operations (add elements at the end of the list)list.pop()– Used to implement pop operations (remove elements at the end)list[::-1]– List slicing is used to print elements in reverse ordertop = len(list) - 1– Length of list minus 1- Stack – LIFO (Last In First Out)
- Push and Pop through one end (Top)
Real-life Analogy
- Plates in a rack
- Undo feature in MS Word
- Browser back button
Applications of Stack
- Undo/Redo operations
- Backtracking (puzzles, mazes)
- Function call management (recursion)
- Expression evaluation (Postfix, Prefix)
Stack Operations
1. Stack Initialization
Example: stack = []
2. push() – Add an element to the stack
Use append() function to add an item on top.
Example: stack.append(item)
3. pop() – Remove the top element from the stack
Removes and returns the last element.
4. peek() – View the top element without removing it
Use negative indexing.
5. isEmpty() – Check if the stack is empty
Usage: len(stack) == 0 or not stack
6. isFull() – (Only for fixed size stacks)
Usage: len(stack) == MAX_SIZE
| Operation | Python Code | Description |
|---|---|---|
| push | stack.append(item) | Adds item to the top |
| pop | stack.pop() | Removes and returns top item |
| peek/top | stack[-1] | Returns top item without removing |
| isEmpty | len(stack) == 0 | Checks if stack is empty |
| isFull | len(stack) == MAX_SIZE | Checks if stack is full (if bounded) |
Stack Errors
Stack Overflow
- Meaning: Trying to add (push) an element to a stack that is already full.
- Happens when: Stack has a fixed size and no more space left.
- Result: Error – "Stack Overflow"
Stack Underflow
- Meaning: Trying to remove (pop) an element from a stack that is empty.
- Happens when: There is nothing to remove.
- Result: Error – "Stack Underflow"
Related Resources
Need Help?
Join our tuition classes for personalized guidance and doubt clearing.
Register for Classes →