Using List as Stack in Python

Python | Stack Implementation using List: In this tutorial, we will learn how to implement a stack using Python lists. Write Python code to implement a stack with various stack operations. By IncludeHelp Last updated : June 25, 2023

Stack

First of all, we must be aware of the Stack - the stack is a linear data structure that works on the LIFO mechanism i.e. Last In First Out (that means the Last inserted item will be removed (popped) first).

Stack basic operations

Thus, to implement a stack, basically, we have to do two things:

  • Inserting (PUSH) elements at the end of the list
  • Removing (POP) elements from the end of the list

i.e. both operations should be done from one end.

Stack implementation using list in Python

In Python, we can implement a stack by using list methods as they have the capability to insert or remove/pop elements from the end of the list.

Stack operations using list object

The following are the stack operations that can be implemented using Python list object and its various methods.

  1. DISPLAY: To display stack elements, iterate the list using the for … in or while loop.
  2. PUSH: To push an item in the stack, use the list.append() method. It appends the item at the end of the list which is the required for stack push operation.
  3. POP: To pop an item from the stack, use the list.pop() method.
  4. TOP: To get the topmost item, use list[-1].
  5. EMPTY: To check whether the stack is empty or not, use the list.count() method or directly check with list == [].

Python program to implement stack using list

# Python program to implement stack
# using list

# Declare a list named as "stack"
stack = [10, 20, 30]
print("Stack elements: ")
print(stack)

# PUSH operation
stack.append(40)
stack.append(50)
print("Stack elements after push opration...")
print(stack)

# POP operation
print(stack.pop(), " is removed/popped...")
print(stack.pop(), " is removed/popped...")
print(stack.pop(), " is removed/popped...")
print("Stack elements after pop operation...")
print(stack)

# TOP operation i.e., to get topmost element
print("Stack TOP:", stack[-1])

# To check whether stack is empty or not
if stack == []:
    print("Stack is empty")
else:
    print("Stack is not empty")

Output

Stack elements: 
[10, 20, 30]
Stack elements after push opration...
[10, 20, 30, 40, 50]
50  is removed/popped...
40  is removed/popped...
30  is removed/popped...
Stack elements after pop operation...
[10, 20]
Stack TOP: 20
Stack is not empty

User-defined functions for each stack operation

# Function to check whether stack 
# is empty or not
def stackISEMPTY(stack):
    if stack == []:
        return True
    else:
        return False

# Stack PUSH operation function
def stackPUSH(stack, item):
    stack.append(item)
    print(item, "is inserted...")

# Stack POP operation function
def stackPOP(stack):
    if stackISEMPTY(stack):
        print("Stack is EMPTY")
    else:
        item = stack.pop()
        print(item, "is popped/removed...")

# Function to display stack elements
def stackDISPLAY(stack):
    if stackISEMPTY(stack):
        print("Stack is empty")
    else:
        print("Stack elements:", stack)

# Function to get stack TOP element
def stackTOP(stack):
    return stack[-1]


# Main Code/ Driver Code

# Empty stack
stack = []

print("Stack initially")
stackDISPLAY(stack)

# PUSH operation
stackPUSH(stack, 10)
stackPUSH(stack, 5)
stackPUSH(stack, 15)
stackPUSH(stack, 50)

print("\nStack after PUSH operation")
stackDISPLAY(stack)

# POP operation
stackPOP(stack)
stackPOP(stack)

# Display stack
print("\nStack after POP operation")
stackDISPLAY(stack)

print("Stack TOP:", stackTOP(stack))

Output

Stack initially
Stack is empty
10 is inserted...
5 is inserted...
15 is inserted...
50 is inserted...

Stack after PUSH operation
Stack elements: [10, 5, 15, 50]
50 is popped/removed...
15 is popped/removed...

Stack after POP operation
Stack elements: [10, 5]
Stack TOP: 5

Python List Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.