Open In App

Implementation of Stack in Python using List

Last Updated : 18 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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)

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.

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))

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.

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())

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.

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())

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.

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:


Next Article
Practice Tags :

Similar Reads