Stack in Python Last Updated : 11 Dec, 2025 Suggest changes Share 182 Likes Like Report A stack is a linear data structure that follows the Last-In/First-Out (LIFO) principle, also known as First-In/Last-Out (FILO). This means that the last element added is the first one to be removed. In a stack, both insertion and deletion happen at the same end, which is called the top of the stack. Python does not have a built-in stack type, but stacks can be implemented in different ways using different data structures, let's look at some of the implementations:1. Using a ListPython lists provide built-in methods that make them suitable for stack operations.The append () method adds an element to the end of the list.The pop () method removes and returns the last element from the list.These operations allow a list to directly support stack-like behavior. Python stack = [] # append() function to push element in the stack stack.append('a') stack.append('b') stack.append('c') print('Initial stack') print(stack) # pop() function to pop element from stack in LIFO order print('\nElements popped from stack:') print(stack.pop()) print(stack.pop()) print(stack.pop()) print('\nStack after elements are popped:') print(stack) # uncommenting print(stack.pop()) will cause an IndexError as the stack is now empty OutputInitial stack ['a', 'b', 'c'] Elements popped from stack: c b a Stack after elements are popped: [] Lists may slow down when they grow very large due to memory reallocation.2. Using collections.dequePython’s collections module provides a deque (double-ended queue) for efficient insertions and deletions.The append () method adds an element to the right end of the deque.The pop () method removes and returns the element from the right end.Since deque is optimized for fast appends and pops, it is often preferred over lists for stack implementation.Example: Python from collections import deque stack = deque() # append() function to push element in the stack stack.append('a') stack.append('b') stack.append('c') print('Initial stack:') print(stack) # pop() function to pop element from stack in LIFO order print('\nElements popped from stack:') print(stack.pop()) print(stack.pop()) print(stack.pop()) print('\nStack after elements are popped:') print(stack) # uncommenting print(stack.pop()) will cause an IndexError as the stack is now empty OutputInitial stack: deque(['a', 'b', 'c']) Elements popped from stack: c b a Stack after elements are popped: deque([]) Stack Implementation using some other Data StructuresUsing queueUsing Linked-List Create Quiz N nikhilaggarwal3 Follow 182 Article Tags : Python Python-DSA Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like