Open In App

Implementation of Stack in Python using List

In Python list is one of the most widely used and popular built-in datatypes that represents ordered and mutable collections of elements. List also allows to storage of duplicate items. Lists are widely used for various tasks in Python programming, such as storing collections of data, implementing stacks and queues, and handling sequences of values.

Using List As Stack In Python

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:

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.




# Created a Empty List
stack = []
 
# Pushing the elements in the list
stack.append(1)
stack.append(2)
stack.append(3)
 
print("The Stack items are :")
for i in stack:
    print(i)

Output
The 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.




stack = []
 
def pop(stack):
    if len(stack) == 0:
        return None
    else:
        return stack.pop()
 
# Pushing the elements in the list
stack.append(1)
stack.append(2)
stack.append(3)
 
print("Initial Stack")
print(stack)
 
print("Popped Element: ", pop(stack))

Output
Initial 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.




stack = []
 
def top():
    if len(stack) == 0:
        return None
    return stack[-1]
 
 
# Pushing the elements in the list
stack.append(1)
stack.append(2)
stack.append(3)
 
print("Top Element of Stack is : ", top())

Output
Top 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.




stack = []
def is_empty():
    if len(stack) == 0:
        return 1
    else:
        return 0
 
 
# Pushing the elements in the list
stack.append(1)
stack.append(2)
stack.append(3)
 
print( is_empty())

Output
0



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.




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()
 
 
my_stack = ["Lokesh", "Diwakar", "Aniket", "Ritik"]
 
# Printing the stack
print(my_stack)
 
# Printing the size of stack
print(st.size(my_stack))
 
# Removing the element of stack
st.pop(my_stack)
 
# Printing the top element of stack
print("Top element in the stack : ", st.top(my_stack))
 
# Check stack is Empty or not
print(st.isEmpty(my_stack))
 
# Push element into stack
st.push(my_stack, "Vimal Kant")
 
# Stack after Push
print(my_stack)

Output
['Lokesh', 'Diwakar', 'Aniket', 'Ritik']
4
('Removed element from the st : ', 'Ritik')
('Top element in the stack : ', 'Aniket')
0
['Lokesh', 'Diwakar', 'Aniket', 'Vimal Kant']




Article Tags :