Python Stack

A Stack is a Last-In-First-Out Linear data structure i.e. the element entered at the last will be the first element to be removed.

In stack, the items are added at one end and these items are removed from the same end.

Operations associated with the Stack:

  • Push – Addition of elements into the stack
  • Pop – Deletion/Removal of elements from the stack

Push operation in Stack

The push() method is used to add elements to the stack. This method adds an element to the top of the stack. We can use append() method to add elements to the stack.

Example:

stack_store = [] 
print('current stack :', stack_store)

for x in range(3):
    # push items into stack
    stack_store.append(x)
    print('current stack :', stack_store,'\tstack size :', len(stack_store))

Output:

Output Push Method
Output-Push Method

Pop operation in Stack

The pop() method is used to remove elements from the stack. This method when called removes the topmost element from the stack.

Example:

stack_store= []
print('\ncurrent stack :', stack_store)

print('\nPushing/Adding items to the stack....')

for x in range(3):
    
    stack_store.append(x)
    print('current stack :', stack_store,'\tstack size :', len(stack_store))

print('\nRemoving items from the stack....')
while len(stack_store) > 0:  
    stack_store.pop()
    print('current stack after pop() operation :', stack_store)

Output:

Output Pop Method
Output-Pop Method

Ways to implement Stack in Python

The following are the ways to implement a stack in Python:

  • list
  • collections.deque
  • queue.LifoQueue

Method 1: Implementing Stack using a List

# stack using list 
stack_list = ["English", "Gujarati", "Hindi"] 
stack_list.append("Marathi") 
stack_list.append("Kannada") 
print(stack_list) 
print(stack_list.pop()) 
print(stack_list) 
print(stack_list.pop()) 
print(stack_list) 

Output:

Output Stack Using List
Output-Stack Using List

Method 2: Implementing Stack using Deque Collection

from collections import deque
stack_store = deque()

stack_store.append('Delhi')
stack_store.append('Satara')
stack_store.append('Gujarat')

print(stack_store)

stack_store.pop()
print(stack_store)
stack_store.pop()
print(stack_store)
stack_store.pop()
print(stack_store)

Output:

Output Deque As Stack
Output-Deque As Stack

Method 3: Implementing Stack using Queue

from queue import LifoQueue 
  
# Initializing a stack 
stack_store = LifoQueue(maxsize = 3) 
  

print(stack_store.qsize()) 
   

stack_store.put('1') 
stack_store.put('2') 
stack_store.put('3') 
  
print("Is the stack full?: ", stack_store.full())  
print("The size of the stack is: ", stack_store.qsize())  
  

print('\nItems poped/removed from the stack: ') 
print(stack_store.get()) 
print(stack_store.get()) 
print(stack_store.get()) 
  
print("\nIs the stack empty?: ", stack_store.empty()) 

Output:

Output Stack Using Queue
Output-Stack Using Queue

Conclusion

Thus, in this article, we have understood the mechanism of the Stack Data structure and its basic operations and vivid ways to implement it in programming.

References