The stack in python is a data structure that follows the last in first out (LIFO) manner. That means the last element entered in the stack comes out first. A stack has one open end from which the elements are added. The last element added in the stack is called the top. The following are the names of operations performed on the stack.
- Push (adding element in the stack)
- Pop (removing element from the stack)
There are two types of stack. The first is a static stack, and the second is a dynamic stack. As python is a dynamic programming language, we can implement dynamic stack quickly. For implementing a static stack, we need to do some validations like the size of the stack, etc.
Table of Contents
How to create a stack in python?
The implementation of a stack in python is very easy. We can implement the stack using list, collections deque, and deque’s LIFO queue. Let’s see the implementation of a stack one by one with examples.
Implementation of stack using list
Python provides an inbuilt data structure called a list. We can use the list data structure to create a stack in python. The list provides many methods because of that, and the implementation gets easier. Some of the list methods are as follows:
- append() : to add element in list
- pop() : to remove element from the list
- insert() : to insert element at specified position
- sort() : to sort the list
- reverse(): to reverse to the sequence of the list
- count(): to count the number of elements in the list
Using the above functions, we can create a stack in python. Let’s understand it with an example.
Example:
mylist = []
while(True):
print("1. push element")
print("2. pop element")
print("3. check size of stack")
print("4. display elements of stack")
print("5. exit")
n = int(input("Enter your choice"))
if(n==1):
x = input("Enter element")
mylist.append(x)
elif(n==2):
print("removed element is : ",mylist.pop())
elif(n==3):
print("Size of stack is : ",len(mylist))
elif(n==4):
print(mylist)
elif(n==5):
exit()
else:
print("invalid choice")
Output
1. push element
2. pop element
3. check size of stack
4. display elements of stack
5. exit
Enter your choice1
Enter element4
1. push element
2. pop element
3. check size of stack
4. display elements of stack
5. exit
Enter your choice1
Enter element5
1. push element
2. pop element
3. check size of stack
4. display elements of stack
5. exit
Enter your choice1
Enter element6
1. push element
2. pop element
3. check size of stack
4. display elements of stack
5. exit
Enter your choice4
['4', '5', '6']
1. push element
2. pop element
3. check size of stack
4. display elements of stack
5. exit
Enter your choice2
removed element is : 6
1. push element
2. pop element
3. check size of stack
4. display elements of stack
5. exit
Enter your choice2
removed element is : 5
1. push element
2. pop element
3. check size of stack
4. display elements of stack
5. exit
Enter your choice5
Implementation of stack using collections deque
The collection deque is a queue, but we can use it as a stack. The functions provided by collections deque are the same as list functions. Let’s understand it by implementing collections deque as a stack in python.
Example:
from collections import deque
stack = deque()
print("1. push element")
print("2. pop element")
print("3. check size of stack")
print("4. display elements of stack")
print("5. exit")
while(True):
n = int(input("Enter your choice "))
if(n==1):
x = input("Enter element")
stack.append(x)
elif(n==2):
print("removed element is : ",stack.pop())
elif(n==3):
print("Size of stack is : ", len(stack))
elif(n==4):
print(stack)
elif(n==5):
exit()
else:
print("invalid choice")
Output
1. push element
2. pop element
3. check size of stack
4. display elements of stack
5. exit
Enter your choice 1
Enter element4
Enter your choice 1
Enter element5
Enter your choice 1
Enter element7
Enter your choice 1
Enter element4
Enter your choice 4
deque(['4', '5', '7', '4'])
Enter your choice 3
Size of stack is : 4
Enter your choice 2
removed element is : 4
Enter your choice 2
removed element is : 7
Enter your choice 4
deque(['4', '5'])
Enter your choice 5
Implementation of stack using LIFO queue.
The LIFO queue is a predefined data structure provided by python. LIFO queue is a queue, but it follows last in first out manner (LIFO). To use the Lifo queue, import it from the package queue and create an object of it. The LIFO queue has some differently named functions, but their use is the same as list and deque. Let’s understand it with an example.
Example:
from queue import LifoQueue
stack = LifoQueue(maxsize=5)
print("1. push element")
print("2. pop element")
print("3. check size of stack")
print("4. Check stack is full or not")
print("5. exit")
while(True):
n = int(input("Enter your choice"))
if(n==1):
x = input("Enter element")
stack.put(x)
elif(n==2):
print("removed element is : ",stack.get())
elif(n==3):
print("Size of stack is : ",stack.qsize())
elif (n == 4):
if(stack.full()):
print("Stack is full")
else:
print("Stack is not full")
elif(n==5):
exit()
else:
print("invalid choice")
Output
1. push element
2. pop element
3. check size of stack
4. Check stack is full or not
5. exit
Enter your choice 1
Enter elementa
Enter your choice 1
Enter element b
Enter your choice 1
Enter element c
Enter your choice 1
Enter element d
Enter your choice 1
Enter elemente
Enter your choice 4
Stack is full
Enter your choice 3
Size of stack is : 5
Enter your choice 2
removed element is : e
Enter your choice 2
removed element is : d
Enter your choice 5
FAQ’s
There are many ways to implement a stack in python. We can use list, deque, and Lifo queue to implement stack in python.
To use stack, we can use the predefined functions. Using functions like push(), pop() or get(), put() makes the implementation way more easier.
Full-stack python is a type of developer. The developer can manage to design front end, back end, and database connectivity using their coding skills.
Did you know? |
---|
1. Encapsulation in Python |
2. Operator Overloading in Python |
3. iloc in Python |
4. Python Pop |
5. Python Enum |
6. Python Pass |
7. Python deque |
8. Python Not Equal Operator |
9. Python Floor |
10. Python Stack |