Stack in Python is a one-dimensional data structure that is also called a Last in First out data structure (LIFO). In a Stack data structure, If an element is inserted at last, it will come out first. In this article, we will discuss three ways to implement a Stack. In each scenario, we will see how to create, push, and pop items from the stack with examples.
Implement Stack using list
Implement Stack using deque()
Implement Stack using LifoQueue
1. Quick Examples of Implement Stack in Several Ways
Following are quick examples of implementing a stack in Python.
# below are some quick examples.
import queue
# Initializing a stack with size 5 using LifoQueue
stack = queue.LifoQueue(maxsize=5)
# Using put() function to insert elements
stack.put(‘China’)
stack.put(‘Russia’)
stack.put(‘England’)
stack.put(‘Nepal’)
stack.put(‘Italy’)
# Return total number of elements
print(stack.qsize())
# Use the get() function to pop element from stack in LIFO order
print(‘3 elements popped from the stack are:’)
print(stack.get())
print(stack.get())
print(stack.get())
# Return total number of elements
print(stack.qsize())
# Declare stack
stack = []
# Use append() method to insert
# elements to the stack.
stack.append(‘China’)
stack.append(‘Russia’)
stack.append(‘England’)
stack.append(‘Nepal’)
print(“Stack: “,stack)
# Use pop() method to pop
# elements from the stack.
print(‘2 elements popped from the stack are:’)
print(stack.pop())
print(stack.pop())
print(“Stack: “,stack)
from collections import deque
# Declare stack using deque
stack = deque()
# Use append() method to insert
# elements to the stack.
stack.append(‘China’)
stack.append(‘Russia’)
stack.append(‘England’)
stack.append(‘Nepal’)
print(“Stack: “,stack)
# Use pop() method to pop
# elements from the stack.
print(‘2 elements popped from the stack are:’)
print(stack.pop())
print(stack.pop())
print(“Stack: “,stack)
2. Implement Stack using List in Python
List in python is a one dimensional data structure which will store multiple data type elements. We can push the elements by using append() method, which is similar to push operation of stack.
Similarly, we can pop item using the pop() method. By this way list can be used as a Stack.
2.1 Syntax for push and pop operations
Here, append() is used to push the element and pop() is used to pop the items.
# Push operation
stack.append(element)
# Pop operation
stack.pop()
2.2 Example
Let’s create a Stack by using List and push 4 countries one by one using the append() method and pop 2 elements.
# Declare stack
stack = []
# Use append() method to insert
# elements to the stack.
stack.append(‘China’)
stack.append(‘Russia’)
stack.append(‘England’)
stack.append(‘Nepal’)
print(“Stack: “,stack)
# Use pop() method to pop
# elements from the stack.
print(‘2 elements popped from the stack are:’)
print(stack.pop())
print(stack.pop())
print(“Stack: “,stack)
# Output:
# Stack: [‘China’, ‘Russia’, ‘England’, ‘Nepal’]
# Nepal
# England
# Stack: [‘China’, ‘Russia’]
Yields below output.
4 countries are pushed into the stack and we popped 2 times. ‘Nepal’ and ‘England’ were popped from stack.
Finally the stack holds ‘China’ and ‘Russia’.
3. Implement Stack using LifoQueue in Python
LifoQueue is available in queue module which will directly implement a Stack. We can pop item using the get() method and push an item using put() method.
put() will take the item as a parameter and get() take no parameters. get() returns the popped item.
3.1 Syntax for push and pop operations
Here, put() is used to push the element and get() is used to pop the items.
# Push operation
stack.put(element)
# Pop operation
stack.get()
3.2 Example
Let’s create a Stack by using LifoQueue with size – 5 and push 5 countries one by one using the put() method and pop 3 elements.
import queue
# Initializing a stack with size 5
stack = queue.LifoQueue(maxsize=5)
# Using put() function to insert elements
stack.put(‘China’)
stack.put(‘Russia’)
stack.put(‘England’)
stack.put(‘Nepal’)
stack.put(‘Italy’)
# Return total number of elements
print(stack.qsize())
# Use the get() function to pop element from stack in LIFO order
print(‘3 elements popped from the stack are:’)
print(stack.get())
print(stack.get())
print(stack.get())
# Return total number of elements
print(stack.qsize())
# Output:
# 5
# 3 elements popped from the stack are:
# Italy
# Nepal
# England
# 2
5 countries are pushed into the stack and we popped 3 times. Finally the stack holds only 2 countries
4. Implement Stack using deque in Python
The deque stands for Double Ended Queue, which is an one dimensional data structure like list in python. In this data structure, we can perform push and pop operations from both ends. We can push the elements by using append() method, which is similar to push operation of stack at one end (right side).
Similarly, we can pop item using the pop() method from right (end). By this way deque can be used as a Stack.
4.1 Syntax for push and pop operations
Here, append() is used to push the element and pop() is used to pop the items.
# Push operation
stack.append(element)
# Pop operation
stack.pop()
4.2 Example
Let’s create a Stack by using deque and push 4 countries one by one using the append() method and pop 2 elements using the pop() method.
from collections import deque
# Declare stack
stack = deque()
# Use append() method to insert
# elements to the stack.
stack.append(‘China’)
stack.append(‘Russia’)
stack.append(‘England’)
stack.append(‘Nepal’)
print(“Stack: “,stack)
# Use pop() method to pop
# elements from the stack.
print(‘2 elements popped from the stack are:’)
print(stack.pop())
print(stack.pop())
print(“Stack: “,stack)
# Output:
# Stack: deque([‘China’, ‘Russia’, ‘England’, ‘Nepal’])
# 2 elements popped from the stack are:
# Nepal
# England
# Stack: deque([‘China’, ‘Russia’])
Now the final stack will hold 3 elements.
5. Conclusion
So we have seen how to implement Stack using list, deque, and LifoQueue. We have seen how to perform push and pop operations on a stack in all scenarios. In the deque scenario, you need to use append() and pop() instead of appendleft() and popleft() methods.
Stack in Python is a one-dimensional data structure that is also called a Last in First out data structure (LIFO). In a Stack data structure, If an element is inserted at last, it will come out first. In this article, we will discuss three ways to implement a Stack. In each scenario, we will see Read More Python, Python Tutorial, Python Stack Examples