You are currently viewing Stack in Python Explained with Examples

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 Python examples.

Advertisements
  1. Implement Stack using list
  2. Implement Stack using deque()
  3. Implement Stack using LifoQueue

1. Implement Stack using List in Python

You can use List type to implement the Stack manually in Python. List in python is a one-dimensional data structure that 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.

Here, append() is used to push the element and pop() is used to pop the items. 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 as a list
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)

Yields below output.

implement stack in python

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’.

2. Using Stack from LifoQueue in Python

LifoQueue is available in queue module which which is implemented as 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.

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

Here, 5 countries are pushed into the stack and we popped 3 times. Finally the stack holds only 2 countries

3. Using Stack from 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.

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.


# Import deque
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 holds 3 elements.

4. Conclusion

So we have seen how to implement Stack using list, deque, and LifoQueue in Python. 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.