Introduction to Stack in Python
Introduction to Stack in Python
Introduction to Stack in Python is a linear data structure that follows the Last-In-First-Out (LIFO) principle, which means that the last element added to the stack is the first one to be removed. You can implement a stack in Python using various data structures, such as lists or collections.deque.
In this page, we will delve into the introduction to stack in python, concepts, understanding their basic principles, real-world applications, and why they are essential for every programmer to grasp.

What is Stack?
A stack is a linear data structure that follows the Last-In, First-Out (LIFO) principle. Imagine a stack of plates in a cafeteria – the last plate added is the first one to be removed. Similarly, in a programming stack, the last element pushed onto the stack is the first one to be popped off.

Introduction to stack in Python
Components of a Stack:
Before we dive deeper, let’s understand the basic components of a stack:
Stack Container
- The stack container is where all the data elements are stored. It can be implemented using arrays or linked lists.
Push Operation
- The “push” operation is used to add an element to the top of the stack.
def push(item): # Add 'item' to the top of the stack
Pop Operation
- The “pop” operation removes the top element from the stack.
def pop(): # Remove and return the top element of the stack
Peek Operation
- The “peek” operation allows you to view the top element without removing it.
def peek(): # Return the top element without removing it

Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Implementing a Stack
Implementing a stack involves using data structures like lists, deque, or LifoQueue to mimic Last-In-First-Out (LIFO) behavior. Each method supports basic operations such as push, pop, and peek, but differs in efficiency, flexibility, and use-case suitability. In DSA, custom stack implementations using lists or deque are most commonly used for clarity and control.
1. Stack Using list
stack = [] # Push operations stack.append(10) stack.append(20) stack.append(30) # Pop operation (removes 30) stack.pop() # Peek (shows 20) print("Top element:", stack[-1])
Output:
Top element: 20
Explanation:
- stack.append(x) adds x to the top of the stack (end of the list).
- stack.pop() removes the last item (LIFO behavior).
- stack[-1] accesses the current top without removing it.
Complexity:
- Time Complexity: O(1) for push/pop/peek
- Space Complexity: O(n) — more memory-efficient than list
2. Stack Using collections.deque
from collections import deque stack = deque() # Push operations stack.append(10) stack.append(20) stack.append(30) # Pop operation (removes 30) stack.pop() # Peek (shows 20) print("Top element:", stack[-1]
Output:
Top element: 20
Explanation:
deque allows O(1) time complexity for adding/removing items from both ends.
append() and pop() behave exactly like in list but are more efficient for large data.
[-1] accesses the last element, which is the top of the stack.
Complexity:
- Time Complexity: O(1) for push/pop/peek
- Space Complexity: O(n) — more memory-efficient than list
3. Stack Using queue.LifoQueue
from queue import LifoQueue stack = LifoQueue() # Push operations stack.put(10) stack.put(20) stack.put(30) # Pop operation (removes 30) stack.get() # Peek workaround (LifoQueue has no direct peek) top = stack.get() # Pop to access print("Top element:", top) stack.put(top) # Push it back to restore state
Output:
Top element: 20
Explanation:
LifoQueue stands for “Last In First Out Queue”.
put(x) is like push, and get() is like pop.
It doesn’t support indexing like [-1], so to peek, we pop the item and immediately push it back.
It’s thread-safe, so preferred in multi-threaded environments (not needed in DSA).
Complexity:
- Time Complexity: O(1) for put/get, O(n) for peek workaround
- Space Complexity: O(n) with built-in thread safety
Advantages of Using Stacks
Stacks are not just abstract data structures; they have real-world applications. Here’s why they are essential.
To wrap it up:
In this page, we’ve explored the stack data structure in-depth. From its fundamental principles and key operations to real-world applications and implementation in Python, you now have the knowledge to leverage stacks effectively in your projects.
FAQs
A stack in Python is a linear data structure that follows the Last In, First Out (LIFO) principle. The most recent element added is the first to be removed.
You can implement a stack using lists, collections.deque, or queue.LifoQueue. Each provides push() and pop() functionality with slight performance and thread-safety differences.
The primary operations are push (to add an item), pop (to remove the top item), peek (to view the top), and isEmpty (to check if the stack is empty).
Stacks are widely used in Python for expression evaluation, undo operations, recursion, and backtracking problems due to their LIFO nature.
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment