Implementation of Stack in Python using List
Last Updated :
18 Apr, 2025
Stack is a linear data structure that follows the LIFO principle which means Last in First Out. In the stack insertion of a new element and removal of an existing element takes place at the same end represented as the top of the stack.
Basic Operations on Stack:
- push():- This is a method to insert an element into the stack
- pop():- This is a method to remove an element from the stack
- top():- This is a method that returns the top element of the stack.
- isEmpty():- This is a method that returns true if the stack is empty else false.
- size():- This is a method that returns the size of the stack.
Below are some of the examples by which we can use the list as a stack in Python:
Push Operation Using List in Python
In this operation, we push data to the stack which is implemented by a list. As we all know stack data structure follows the LIFO principle i.e., Last in First Out.
Python
stack = []
stack.append(1)
stack.append(2)
stack.append(3)
print("The Stack items are:")
for i in stack:
print(i)
OutputThe Stack items are:
1
2
3
Pop Operation Using List in Python
The pop Operation in stack use to remove the last element from the stack. Here, we will use pop() function to perform pop operation in list. If the stack is empty it return none.
Python
stack = []
def pop(stack):
if len(stack) == 0:
return None
else:
return stack.pop()
stack.append(1)
stack.append(2)
stack.append(3)
print("Initial Stack")
print(stack)
print("Popped Element:", pop(stack))
OutputInitial Stack
[1, 2, 3]
Popped Element: 3
Top Operation Using List in Python
The top() method of stack used to print the topmost element of the stack. It return the last or recently pushed element from the last.
Python
stack = []
def top():
if len(stack) == 0:
return None
return stack[-1]
stack.append(1)
stack.append(2)
stack.append(3)
print("Top Element of Stack is:", top())
OutputTop Element of Stack is: 3
isEmpty() Operation Using List in Python
The isEmpty method of stack used to check the stack is empty or not. If the stack is empty it return 1 else it returns 0.
Python
stack = []
def is_empty():
if len(stack) == 0:
return 1
else:
return 0
stack.append(1)
stack.append(2)
stack.append(3)
print(is_empty())
Creating a Class and Implementing Stack Method
In this example, we will implement a stack class and we will implement all the stack opertion like push, pop, top, empty, and size by the help of class method.
Python
class stack:
# Method for pushing data into stack
def push(self, st, data):
st.append(data)
# Method for pop
def pop(self, st):
if len(st) > 0:
ans = st.pop()
print("Removed element from the st:", ans)
else:
print("The stack is Empty")
# Method to get top of stack
def top(self, st):
if len(st) > 0:
return st[len(st) - 1]
# Method to check stack is Empty or not
def isEmpty(self, st):
if len(st) == 0:
return 1
else:
return 0
# Method to get size of stack
def size(self, st):
return len(st)
# Creating Object of stack class
st = stack()
s = ["Lokesh", "Diwakar", "Aniket", "Ritik"]
print(s)
print(st.size(s))
st.pop(s)
print("Top element in the stack:", st.top(s))
print(st.isEmpty(s))
st.push(s, "Vimal Kant")
print(s)
Output['Lokesh', 'Diwakar', 'Aniket', 'Ritik']
4
('Removed element from the st : ', 'Ritik')
('Top element in the stack : ', 'Aniket')
0
['Lokesh', 'Diwakar', 'Aniket', 'Vimal Kant']
Explanation:
- push(self, st, data): Adds an element to the top of the stack (list) using append().
- pop(self, st): Removes the top element from the stack using pop(), and prints the removed element. It also checks if the stack is empty before performing the operation.
- top(self, st): Returns the top element of the stack without removing it. It checks if the stack is non-empty.
- isEmpty(self, st): Returns 1 if the stack is empty, and 0 if it's not.
- size(self, st): Returns the current size of the stack by using len().
Relates Articles:
Similar Reads
Python Program to Implement Stack Using Linked List In Python, creating a stack using a linked list involves implementing a data structure where elements are added and removed in a last-in-first-out (LIFO) manner. This approach uses the concept of nodes interconnected by pointers, allowing efficient insertion and deletion operations. We are given a L
4 min read
Implement Stack Using Deque in Python In Python, Stack, and collections. deque is a foundational data structure used for managing elements in a Last-In-First-Out (LIFO) order. In this article, we will learn to create a stack using collections. deque in Python. Example : Input : Stack : 1 2 4 Push(5)Output : Stack : 1 2 4 5Input : Stack
3 min read
Append Elements to Empty List in Python In Python, lists are used to store multiple items in one variable. If we have an empty list and we want to add elements to it, we can do that in a few simple ways. The simplest way to add an item in empty list is by using the append() method. This method adds a single element to the end of the list.
2 min read
Flatten List to Individual Elements - Python We are given a nested list containing sublists, and our task is to convert it into a single list where all elements are at the same level. For example, given a = [[1, 2], [3, 4], [5, 6]], the output should be [1, 2, 3, 4, 5, 6]. Let's discuss different methods to do this in Python.Using List Compreh
3 min read
How to Use Lists as Stacks and Queues in Python In Python, lists can be used to implement a variety of data structures, including stacks and queues. In this article, weâll explore how to use Python lists to create and perform basic operations on stacks and queues.Using List as Stack in PythonA stack is a fundamental data structure that follows th
4 min read